需要点击使用vba网站上的searchbutton

我尝试了几个方法来使用vbamacros单击提交button,但没有一个方法为我工作。 最后,我决定在这里发表我的关注,寻求build议。

该网站是realtor.com

我使用vba在excel表格中填入search框。 现在我无法击中searchbutton。 我试着用下面的vba代码

Dim objTag As Object For Each objTag In html.getElementsByTagName(strTagName) If InStr(objTag.outerHTML, "btn btn-primary js-searchButton") > 0 Then objTag.submit Exit For End If Next 

但它对我没有用。

HTML代码是

 <form class="inline-form" action="/search" accept-charset="UTF-8" method="get"><input name="utf8" type="hidden" value="✓" /> <div class="input-group search-input-group search-input-prepopulate"> <input name="q" type="text" id="searchBox" class="form-control user-input-box" placeholder="Address, City, Zip, or Neighborhood" value="" autocomplete="off" autocorrect="off" autocapitalize="off" spellcheck="false" /> <input id="autocomplete" type="text" class="form-control pre-populate-box" tabindex="-1"/> <span class="input-group-btn"> <button id="clearInput" class="btn btn-link btn-clear-input hide"> <i class="ra ra-close-thin"></i> </button> <button class="btn btn-primary js-searchButton" type="submit"> <i class="ra ra-search"></i> <span class="hidden-xxs hidden-xs">Search</span> </button> </span> </div> </form> 

“如果有人帮我在这里,这将是一个很大的帮助。 提前致谢。

你能从这里调整示例代码吗?

 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 

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

正如罗宾先前build议的那样,您可能想尝试下面的内容:

 Set searchbutton = html.getElementsByClassName("btn btn-primary js-searchButton").Item searchbutton.Click 

下面的代码可能会帮助你解决这个问题: – 注意下面你可以用(0),(1)等selectsearchbutton

 Sub Realtor() Dim ie As InternetExplorer Dim inputfield As HTMLInputElement Set ie = New InternetExplorer ie.Visible = True ie.navigate "realtor.com" Do While ie.readyState <> READYSTATE_COMPLETE Application.StatusBar = "Loading Web page …" DoEvents Loop Set html = ie.document 'This steps through the search boxes one by one an populates them For Each inputfield In ie.document.getElementsByTagName("input") If inputfield.Type = "text" Then inputfield.Value = "seattle" Next Application.Wait (Now + #12:00:05 AM#) 'you can select the search button with (0), (1) etc below Set searchbutton = html.getElementsByClassName("btn btn-primary js-searchButton").Item(0) ' <-- replace this with a 1 or 2 to select search button searchbutton.Click End Sub