需要使用vba点击网站中的搜索按钮

时间:2017-03-19 12:05:05

标签: javascript excel vba excel-vba

我尝试了几种使用vba宏单击提交按钮的方法,但没有一种方法适合我。最后,我决定在此发表我的关注以寻求建议。

该网站是realtor.com

我使用vba从excel表填充了我的数据搜索框。现在我无法点击搜索按钮。我尝试使用以下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="&#x2713;" />
<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>

` 如果有人帮助我,这将对我有很大的帮助。提前谢谢。

3 个答案:

答案 0 :(得分:0)

您可以从这里调整示例代码吗?

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

答案 1 :(得分:0)

你可能想尝试像Robin之前建议的那样: -

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

答案 2 :(得分:0)

以下代码可能会帮助您解决这个问题: - 请注意,您可以选择带有(0),(1)等的搜索按钮

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