通过.Click激活搜索按钮 [英] Activating a search button via .Click

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

问题描述

我正在使用HMTL对象库和Internet控件引用已激活的IE11.

按钮上没有元素ID,但是由于

VBA:

  Option Explicit子HGScrape()'Application.ScreenUpdating = False'我们定义了基本变量昏暗即对象昏暗的my_url作为字符串昏暗的SearchButton,NameBox,AddressBox作为对象的暗淡昏暗x整数昏暗的整数Dim IsOdd为整数昏暗html_doc作为对象'HTMLDocument昏暗的xml_obj作为对象'MSXML2.DOMDocumentmy_url =" https://www.healthgrades.com/"'添加"Microsoft Internet Controls"在您的VBA项目中间接引用设置ie = New InternetExplorerie.Visible = True'False''''''''''''''''''''''即,导航my_url'"13.33.74.92"'("https://www.healthgrades.com/")而ie.ReadyState<>4DoEvents温德设置NameBox = ie.Document.getElementById("search-term-selector-child")NameBox.Value = ActiveSheet.Range("A2")设置AddressBox = ie.Document.getElementById("search-location-selector-child")AddressBox.Value = ActiveSheet.Range("B2")设置html_doc = CreateObject(" htmlfile")设置xml_obj = CreateObject("MSXML2.XMLHTTP")xml_obj.打开"GET",my_url,Falsexml_obj.sendhtml_doc.body.innerHTML = xml_obj.responseText设置SearchButton = ie.Document.getElementsByClassName("autosuggest_submiter")'按钮控件(HTML控件)的IDSearchButton.Click而ie.ReadyState<>4DoEvents温德 

解决方案

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

使用ClassName submiter__text 来获取您的提交"按钮,它的索引为 0 .

  Sub HGScrape()const sURL As String ="https://www.healthgrades.com/"昏暗即作为新的InternetExplorer与即.Visible = True.导航sURL而.Busy或.ReadyState<4:DoEvents:Wend.Document.getElementById("search-term-selector-child")._值= ActiveSheet.Range("A2").Document.getElementById("search-location-selector-child")._值= ActiveSheet.Range("B2").Document.getElementsByClassName("submiter__text")(0).点击而.Busy或.ReadyState<4:DoEvents:Wend结束于结束子 

".为什么"submitter_text"类是正确的类?"

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

I'm using IE11 with the HMTL Object Library and Internet Controls references activated.

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.

This is where my error occurs.

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

解决方案

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.

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

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

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.

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

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