Chum Ba
Chum Ba

Reputation: 47

VBA can't access nested span element in HTML

I'm having issues with accessing a button with a span tag because it is nested within multiple tags. Here is a screenshot of the code with the highlighted portion that I'm trying to click on. [1]: https://i.sstatic.net/yaPIk.jpg "HTML code"

I've already tried getting all of the spans element by using elementcollection but it still cannot access the span element that I need.

Dim HTMLspans As MSHTML.IHTMLElementCollection

   Set HTMLspans = HTMLDoc.getElementsByTagName("span")
     For Each HTMLspan In HTMLspans
        Debug.Print HTMLspan.getAttribute("id")

This code will show me some span elements but not all. I also tried using nested for loops to see if I can access it like this but it still doesn't work.

For Each HTMLtable In HTMLtables.getElementsByTagName("table")
 For Each HTMLtbody In HTMLtable.getElementsByTagName("tbody")
  For Each HTMLtr In HTMLtbody.getElementsByTagName("tr")
   For Each HTMLtd In HTMLtr.getElementsByTagName("td")
    For Each HTMLspan In HTMLtd.getElementsByTagName("span")
          Debug.Print HTMLspan.getAttribute("id")
          Next HTMLspan
       Next HTMLtd
     Next HTMLtr
    Next HTMLtbody
   Next HTMLtable

This returns some span element but it doesn't show the one that I need.

With the right code, I expect to access the span tag with the id="revit_form_ComboButton_0_label" But I can't access it. Could this code be the one that's causing issues?

<!--Portlet-Outlined Start-->

I cannot access any tags under this code and it's unfortunate that the website I'm using is not public.

Upvotes: 0

Views: 236

Answers (2)

Chum Ba
Chum Ba

Reputation: 47

This is the code I used to access the button on the iframe.

    Dim HTMLDoc As MSHTML.HTMLDocument
    Dim iframeDoc As MSHTML.HTMLDocument
    Dim HTMLbutton As MSHTML.IHTMLElement

    Set iframeDoc = HTMLDoc.frames("eZlmIFrame_iframe").Document
    Set HTMLbutton = iframeDoc.getElementById("revit_form_ComboButton_0_label")
    HTMLbutton.Click

Upvotes: 1

QHarr
QHarr

Reputation: 84465

Just use the id (unless there is a parent iframe/frame)

ie.document.getElementById("revit_form_ComboButton_0_label")

or

ie.document.querySelector("#revit_form_ComboButton_0_label")

Upvotes: 1

Related Questions