VBA Web Scraper:无法通过.Click激活搜索按钮

时间:2018-11-01 20:22:09

标签: html excel-vba button web-scraping getelementsbyclassname

希望这是一个非常简单的问题。我正在使用cmdIE11引用已激活的HMTL Object Library。我对VBA不太满意,因此代码中可能会有很多杂音。

按钮上没有元素ID,但是由于this post的支持,因此可以通过添加一些html和xml声明来使用Internet Controls。我要输入一个名称和城市,然后从excel中输入州名,然后将其插入网站,然后单击搜索按钮,但这是发生我的错误的地方。经典的运行时错误ie.Document.getElementsByClassName知道如何解决和继续吗?

HTML: enter image description here

VBA:

'438': Object doesn't support this property or method.

1 个答案:

答案 0 :(得分:3)

我简化了您的代码。您确实不需要将每个元素都设置为变量。从长远来看,这只会浪费资源。

使用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
  

”。为什么“ submitter_text”类是正确的类?”

解释它的最好方法是向您展示。如果不确定要进行什么选择,请右键单击该元素,然后选择“检查元素”,然后在高亮显示的行中查找。

enter image description here

相关问题