Search code examples
htmlexcelvbagetelementsbyclassname

VBA GetElementsByClassName gets error 5002


First time I'm asking a question so here we go..

I have some experience in VBA but now I've been requested to do some web scraping for what I' ve been reading and doing some research in HTML as well.. the thing is I have a problem I can't solve.

There is really no difficulties in what I need to do.. just get some elements like buttons to be clicked or input forms to be auto completed but this one is the one I can't access to:

<div class="dojoPopupMenu2" style="left: 31px; top: 293px; z-index: 1001;" dojoattachpoint="containerNode">
 <ul dojoattachpoint="containerNode">
  <li class="dojoMenuItem2 " dojoattachevent="onMouseOver: onHover; onMouseOut: onUnhover; onclick: _onclick; onKey:onKey;" dojoinsertionindex="0"><span tabindex="-1" class="dojoMenuItem2Label">1</span></li>
 <li class="dojoMenuItem2 " dojoattachevent="onMouseOver: onHover; onMouseOut: onUnhover; onclick: _onclick; onKey:onKey;" dojoinsertionindex="1"><span tabindex="-1" class="dojoMenuItem2Label">2</span></li>
 <li class="dojoMenuItem2 " dojoattachevent="onMouseOver: onHover; onMouseOut: onUnhover; onclick: _onclick; onKey:onKey;" dojoinsertionindex="2"><span tabindex="-1" class="dojoMenuItem2Label">3</span></li>
 <li class="dojoMenuItem2" dojoattachevent="onMouseOver: onHover; onMouseOut: onUnhover; onclick: _onclick; onKey:onKey;" dojoinsertionindex="3"><span tabindex="-1" class="dojoMenuItem2Label">4</span></li>
 <li class="dojoMenuItem2" dojoattachevent="onMouseOver: onHover; onMouseOut: onUnhover; onclick: _onclick; onKey:onKey;" dojoinsertionindex=`enter code here`"4"><span tabindex="-1" class="dojoMenuItem2Label">5</span>
 </li>
</ul>
</div>

There is a previous button called "Add Items" that when it's clicked you get a dropdown list with numbers 1 to 5 (code above) and That's what I need to select.. these numbers depending on some simple conditionals

I have tried these options but none seem to work

Dim ieApp As Object
Dim ieDoc As Object
Dim ieEl As HTMLDivElement
Dim ieEls As HTMLObjectElement
Dim direccion As String
Set ieApp = CreateObject("InternetExplorer.Application")

With ieApp
For Each element In .document.getElementsByTagName("div") 'Tried with the tag "li" as well
    if element.classname = "dojoMenuItem2" Then
        'set a variable with the number 1 to 5 from the list (depending on other condition)
    end if
Next
End With


'also tried this
Dim additems as HTMLDivElement
Set additems = ieDoc.GetElementsByClassName("dojoMenuItem2")(0)
'this gets Automate error

'also tried this
Dim additems as HTMLDivElement
Set additems = ieDoc.GetElementsByClassName("dojoPopupMenu2")(0)
'this gets Automate error

Nothing seems to work so I don't know what I'm not seeing.

Thanks!


Solution

  • Have you tried to apply a CSS attribute selector?

    ieApp.document.querySelectorAll("[dojoinsertionindex]")  
    

    The attribute selector [dojoinsertionindex] will return all elements with attribute [dojoinsertionindex].

    This returns a nodeList you can index into.

    Or loop the length of:

    Dim aNodeList As Object, i As Long
    Set aNodeList = ie.document.querySelectorAll("[dojoinsertionindex]")  
    For i = 0 To aNodeList.Length-1
        Debug.Print aNodeList.item(i).innerText '< === as check
    Next i
    

    Click a partic index (starts at 0 for 1)

    aNodeList.item(0).Click
    

    CSS selector on your HTML sample:

    query