Search code examples
vbainternet-explorerbuttonsearchonclick

VBA - Click on a button in IE that appears not to be a Button


I am trying to run a vba code that: 1 - access a site (https://www.protestosp.com.br/consulta-de-protesto/) 2 - enter some information 3 - Click on a button "Consultar" (here is the problem)!

Problem description: I am not able to click on the button (I am thinking this is not a button, but I dont really know).

<input class="btn-padrao blue borderEffect2 mt-3 hoverEffect wider3" type="button" onclick="ValidarConsulta(this);" value="CONSULTAR">

in my current code, i am using:

```Set frm = IE.Document.GetElementsByclass("btn-padrao blue borderEffect2 mt-3 hoverEffect wider3")
frm.submit```

Solution

  • I suggest trying to loop through input elements on the page and check its value. If the value is CONSULTAR then try to click it.

    Example VBA code:

    Sub demo()
    
        Dim IE  
        Set IE = CreateObject("InternetExplorer.Application")
        IE.Visible = True
        IE.navigate "https://Your_URL_will_be_here..."
    
        Do While IE.Busy
            Application.Wait DateAdd("s", 1, Now)
        Loop
         
       Set clickEvent = IE.document.createEvent("HTMLEvents")
       clickEvent.initEvent "click", True, False
       
       Set elem1 = IE.document.getElementById("AbrangenciaNacional")
       elem1.Click
       elem1.dispatchEvent clickEvent
       
       IE.document.getElementById("TipoDocumento").Value = "2"
       
       IE.document.getElementById("Documento").Value = "12345"
    
       'Below is code to loop through inputs, find the button and click it.
        Set elems = IE.document.getElementsByTagName("input")
        For Each elem In elems
            If (elem.Value) = "CONSULTAR" Then
                elem.Click
                elem.dispatchEvent clickEvent
                Exit For
            End If
        Next elem
    
    End Sub 
    

    Output:

    enter image description here

    You can see that code is clicking the CONSULTAR button and the page is showing the error because the Document number is not valid.

    You can try to pass the correct values and try to test this code on your side.