2016-09-08 5 views
1

VBAを使用してウェブページのボタンをクリックしようとしています。VBAでWebページのボタンを押す

<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") 

これは、オブジェクトに必要なエラーも示します。

編集:ユーザーJordanが指摘している検索文字列を修正しました。 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

このようなクエストの典型的な例は次のとおりですに。

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 
関連する問題