0

I have form which I would like to open IE window, navigate to specific web page, input some data, click the submit button and capture results.

I have been stuck at clicking the submit button part for days and search multiple forums for answer but no luck. The reason I believe is because it's not a true button and does not have a name tag or id I can reference

The site that I am accessing is http://trulos.com/mileage/

inspect code from chrome for the button

Calculate Mileage

I can input the Origin address and Destination address but just can not figure out how to click the "Calculate mileage button.

Sample code I am working with

Dim frominput As String
Dim toinput As String
frominput = "Stockton, CA"
toinput = "Lubbock, TX"
Dim ele As Object


Dim IE As Object
Set IE = CreateObject("internetexplorer.application")
IE.navigate "http://trulos.com/mileage/"
IE.Visible = True

While IE.busy
    DoEvents
Wend
Pause (2)






IE.Document.all("from").Value = frominput
IE.Document.all("to").Value = toinput
'IE.Document.Forms(0).submit
'IE.Document.all("button:Calculate Mileage").Click
'IE.Document.all("btnG").Click


'IE.Document.getElementsByTagName("button")(0).Click
'Set ele = IE.Document.getElementsByClassName("Panel-Body")(0)
'MsgBox ele.classname



'<button type="button" onclick="get_addresses();" class="btn btn-default"> Calculate Mileage</button>
'MsgBox IE.Document.body.innertext
'MsgBox IE.Document.getElementsByClassName("btn btn-default")


'Set the_input_elements = IE.Document.getelementsbytagname("input")
'For Each input_element In the_input_elements
'If input_element.getattribute("class") = "btn btn-default" Then
'    input_element.Click
'    Exit For
'End If
'Next input_element



'Set tags = IE.Document.getElementsByTagName("button")
Set tags = IE.Document.getElementsByClassName("btn btn-default")
For Each tagx In tags
    If tagx.innertext = "submit" Then
        tagx.Click
        Exit For
    End If
MsgBox tagx.innertext
Next
Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343
Jerry
  • 1
  • 1

1 Answers1

2

CSS selectors:

I would use a CSS selector to target the onclick attribute of the button via its value. The [] is an attribute selector. I am using the combination syntax of [attr=value]. So, I am looking for an element with attribute onclick, whose value is 'get_addresses();'. You apply it via the querySelector method as the first matching element is the only one required (and present!). The attribute value is unique on the page and this is a fast selector method, particularly with later versions of IE.

ie.document.querySelector("[onclick='get_addresses();']").Click

HTML of button - target attribute:

If you inspect the button HTML you can view the attribute being targeted:

enter image description here

CSS query:

enter image description here


Other options (there are many more) include:

ie.document.querySelector("#form_addresses button").Click
ie.document.querySelectorAll("[type=button]")(6).Click
ie.document.getElementsByTagName("form")(2).getElementsByTagName("button")(0).Click

The second and third target collections of elements. The second returns a nodeList whereas the third returns a DispHTMLElementCollection. The first returns a single element.

QHarr
  • 83,427
  • 12
  • 54
  • 101