Search code examples
htmlexcelvbaweb-scrapinggetelementsbyclassname

Activating a search button via .Click


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:
enter image description here

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

Solution

  • 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.

    enter image description here