用VBA按网页上的button

我正在尝试使用VBA单击网页上的button。 该button位于<button>标记内,如下面的代码所示:

 <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 

这给了我一个对象所需的错误。 我也试过使用:

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

这也给一个对象所要求的错误。

编辑:更正用户Jordan指出的searchstring。 Debug.Print不执行,因此在使用.getElementsByName查找元素时可能已经出现错误。 该脚本已经能够打开页面并在点击button之前在search框中input文本。

首先,您正在search索引中不存在的元素。 在您提供的html示例中,没有名为“button”的元素集合。

其次,你在当前代码中search的类名是:

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

但是在你的html例子中,button的类名是:

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

尝试用你的For循环replace:

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

这是这类问题的典型例子。

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 = "sample@vbadud.com" 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