简体   繁体   中英

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://imgur.com/a/e7Ya412 "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.

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")

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM