2016-09-08 2 views
1

VBA를 사용하여 웹 페이지의 버튼을 클릭하려고합니다.VBA로 웹 페이지의 버튼 누르기

<div class="search-options-container"> 
<button class="aui-item aui-button aui-button-subtle search-button" 
type="button" original-title="Search for Issues"> 
<span class="aui-icon aui-icon-small aui-iconfont-search">Search</span></button> 
</div> 

지금까지 사용하고있는 코드는 다음과 같습니다 :

Dim ieApp As New SHDocVw.InternetExplorer 
Dim ieElement As Object 
Dim oHTML_Element As IHTMLElement 
... 

Do While ieApp.Busy And Not ieApp.readyState = READYSTATE_COMPLETE 
    DoEvents 
Loop 

For Each oHTML_Element In ie.Document.getElementsByName("button") 
    Debug.Print ("REACHED") 
    If oHTML_Element.className = "aui-item aui-button aui-button-subtle search-button" Then 
    oHTML_Element.Click 
    End If 
Next 

나에게 개체 필요한 오류를주는 아래 냈다 코드에서와 같이 버튼은 <button> 태그에 있습니다. 나는 또한 사용하려고 시도 :

ieElement = ieApp.Document.getElementsByTagName("button") 

또한 개체 필요 오류를 제공합니다.

편집 : 사용자가 요르단에 의해 지적한대로 검색 문자열을 수정했습니다. Debug.Print가 실행되지 않으므로 .getElementsByName을 사용하여 요소를 찾을 때 오류가 발생했을 수 있습니다. 스크립트는 이미 버튼을 클릭하기 전에 검색 창에 페이지를 열고 텍스트를 입력 할 수 있습니다.

답변

1

첫째, 존재하지 않는 색인의 요소를 검색하고 있습니다. html 예제에서 "button"이라는 요소 컬렉션이 제공되지 않았습니다.

둘째, 당신은 당신의 현재 코드를 검색하는 클래스 이름은 다음과 같습니다

"aui-item aui-button-subtle search-button" 

그러나 당신의 HTML의 예에서 버튼의 클래스 이름은 다음과 같습니다

"aui-item aui-button aui-button-subtle search-button" 

당신의 For를 교체하십시오 루프 :

ieApp.Document.getElementsbyClassName("aui-item aui-button aui-button-subtle search-button")(0).Click 
+0

이런 종류의 오류 때문에 프로그래밍이 왜 싫어할 수 있습니다. 그것은 여전히 ​​오류를 제공합니다. – TheCharlatan

+2

또한 코드에서 'ie'객체가 없다는 것을 알 수 있습니다. 'ieApp'가 아닌 'ieApp'를 사용해야합니까? – Zac

+0

제안 된 대체품은 여전히 ​​오브젝트 요구 오류를 프롬프트합니다. – TheCharlatan

1

다음은 이러한 종류의 questi에 대한 고전적인 예입니다. 에. 여기

http://vbadud.blogspot.com/2009/08/how-to-login-to-website-using-vba.html

그리고 모든 코드입니다.

Dim HTMLDoc As HTMLDocument 
Dim oBrowser As InternetExplorer 
Sub Login_2_Website() 

Dim oHTML_Element As IHTMLElement 
Dim sURL As String 

On Error GoTo Err_Clear 
sURL = "https://www.google.com/accounts/Login" 
Set oBrowser = New InternetExplorer 
oBrowser.Silent = True 
oBrowser.timeout = 60 
oBrowser.navigate sURL 
oBrowser.Visible = True 

Do 
' Wait till the Browser is loaded 
Loop Until oBrowser.readyState = READYSTATE_COMPLETE 

Set HTMLDoc = oBrowser.Document 

HTMLDoc.all.Email.Value = "[email protected]" 
HTMLDoc.all.passwd.Value = "*****" 

For Each oHTML_Element In HTMLDoc.getElementsByTagName("input") 
If oHTML_Element.Type = "submit" Then oHTML_Element.Click: Exit For 
Next 

' oBrowser.Refresh ' Refresh If Needed 
Err_Clear: 
If Err <> 0 Then 
Debug.Assert Err = 0 
Err.Clear 
Resume Next 
End If 
End Sub