通过 .Click 激活搜索按钮

Activating a search button via .Click

我正在使用 IE11,并激活了 HMTL 对象库和 Internet 控件引用。

按钮上没有元素 ID,但由于 this post.

,我可以通过添加一些 html 和 xml 声明来使用 ie.Document.getElementsByClassName

我从 Excel 中获取姓名和城市、州并将其插入网站,然后单击搜索按钮。

这是我的错误发生的地方。

Run-time error '438': Object doesn't support this property or method.

HTML:

VBA:

Option Explicit
Sub HGScrape()
    
    'Application.ScreenUpdating = False
    
     'we define the essential variables
     Dim ie As Object
     Dim my_url As String
     Dim SearchButton, NameBox, AddressBox
     Dim ele As Object
     Dim x As Integer
     Dim y As Integer
     Dim IsOdd As Integer
     
     Dim html_doc As Object 'HTMLDocument
     Dim xml_obj As Object 'MSXML2.DOMDocument
     
     my_url = "https://www.healthgrades.com/"
    
    'add the "Microsoft Internet Controls" reference in your VBA Project indirectly
     Set ie = New InternetExplorer
     ie.Visible = True 'False ''''''''''''''''''''''
     ie.Navigate my_url '"13.33.74.92"     '("https://www.healthgrades.com/")
     While ie.ReadyState <> 4
         DoEvents
     Wend
         
     Set NameBox = ie.Document.getElementById("search-term-selector-child")
     NameBox.Value = ActiveSheet.Range("A2")
         
     Set AddressBox = ie.Document.getElementById("search-location-selector-child")
     AddressBox.Value = ActiveSheet.Range("B2")
         
     Set html_doc = CreateObject("htmlfile")
     Set xml_obj = CreateObject("MSXML2.XMLHTTP")

     xml_obj.Open "GET", my_url, False
     xml_obj.send
     html_doc.body.innerHTML = xml_obj.responseText
         
     Set SearchButton = ie.Document.getElementsByClassName("autosuggest_submiter") 'id of the button control (HTML Control)
     SearchButton.Click
     While ie.ReadyState <> 4
         DoEvents
     Wend

我压缩了你的代码。您真的不需要将每个元素都设置为变量。这只会浪费资源 运行.

使用 ClassName submiter__text 获取您的提交按钮,它是 0 的索引。

Sub HGScrape()

    Const sURL As String = "https://www.healthgrades.com/"

    Dim ie As New InternetExplorer

    With ie

        .Visible = True
        .Navigate sURL
        While .Busy Or .ReadyState < 4: DoEvents: Wend
        .Document.getElementById("search-term-selector-child"). _
                    Value = ActiveSheet.Range("A2")
        .Document.getElementById("search-location-selector-child"). _
                    Value = ActiveSheet.Range("B2")
        .Document.getElementsByClassName("submiter__text")(0).Click
        While .Busy Or .ReadyState < 4: DoEvents: Wend

    End With

End Sub

"..Why was the "submitter_text" class the correct one?"

解释它的最好方法是向您展示。如果您不确定要进行什么选择,请右键单击该元素并选择 "Inspect Element" 并查看突出显示的行。