0

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.stack.imgur.com/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.

QHarr
  • 83,427
  • 12
  • 54
  • 101
Chum Ba
  • 47
  • 6

2 Answers2

1

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")
QHarr
  • 83,427
  • 12
  • 54
  • 101
  • Sorry about the link. I didn't know how to do it properly since I'm new to this. You were correct. The page did have an iframe. Sorry for not mentioning this as I did not know at the time what an iframe was but with more reading I was able to access it by following posts that you also commented in (https://stackoverflow.com/questions/44902558/accessing-object-in-iframe-using-vba) (https://stackoverflow.com/questions/53951031/access-elements-inside-iframe-using-vba). Thank you for being so helpful! – Chum Ba May 15 '19 at 13:17
1

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
Chum Ba
  • 47
  • 6