In this example, we are using the popular Chrome Browser, although many other browsers have the same method for inspecting HTML elements.
Locate the element in question (in our case, the “Post Your Question” button).
Right-click the element
Chose the drop-down item Inspect
This will bring up the F12 Developer Tools. This toolbox will automatically highlight your element.
As you can see, your element has a few options to reference it. The best method is using its ID, as this is always a unique value.
This element’s ID is submit-button-42, which you found in this:
In VBA, you can set a variable to this element by:
Dim IE As InternetExplorer, myElement As Object Set IE = New InternetExplorer Set myElement = IE.document.getElementByID(“submit-button-42”)
Unfortunately, not every element has an ID. This element is included in a couple collections as well. The issue with collection items is that many other elements may also be included within that same collection – meaning that it’s not a unique value.
In our example, there are two collections we can refer to: class and tag. The ClassName is btn__with-icon, which we see from:
and the TagName is button (which is listed directly before the class in the same example above).
In VBA, you can set a variable to these collections as such:
Dim IE As InteretExplorer, ieClassColl As Object, ieTagColl As Object Set ieClassColl = IE.document.getElementsByClassName(“btn__with-icon”) Set ieTagColl = IE.document.getElementsByTagName(“button”)
Remember, these are collection items. This means to reference our exact button in question, we either need to know its index, or we can loop with a For…Each statement
If you know the index # is 0, then use that:
Set ieClassBtn = IE.document.getElementsByClassName(“btn__with-icon”)(0)
Otherwise, you can loop through them:
Dim o As Object, oColl As Object Set oColl = IE.document.getElementsByTagName(“button”) For Each o In oColl If o.innerText = “Post Your Question And Answer” Then Exit For End If Next