VBA Web Scraper:无法通过.Click激活搜索按钮 [英] VBA Web Scraper: Having trouble activating a search button via .Click

查看:124
本文介绍了VBA Web Scraper:无法通过.Click激活搜索按钮的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

希望这是一个非常简单的问题.我在激活HMTL Object LibraryInternet Controls引用的情况下使用IE11.我对VBA不太满意,因此我的代码中可能会有很多杂音.

Hopefully this is a really easy question. I'm using IE11 with the HMTL Object Library and Internet Controls references activated. I'm not very good with VBA so there might be a lot of noise in my code.

按钮上没有元素ID,但是由于'438': Object doesn't support this property or method.您知道如何修复和继续吗?

There's no element ID on the button but am able to use ie.Document.getElementsByClassName by adding some html and xml declarations thanks to this post. I'm taking a name and city, state from excel and plugging it into the website then clicking the search button but this is where my error occurs. Classic Run-time error '438': Object doesn't support this property or method. Any idea how to fix and move on?

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

推荐答案

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

I condensed your code a bit. You really do not need to set every element to a variable. This just wastes resources in the long run.

使用ClassName submiter__text抓住您的提交"按钮,它的索引为0.

Use the ClassName submiter__text to grab your submit button, and it's index of 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"类是正确的类?"

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

The best way to explain it is to show you. If you are unsure what selection to make, then right-click the element and choose "Inspect Element" and look around the highlighted line.

这篇关于VBA Web Scraper:无法通过.Click激活搜索按钮的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆