excelvbagetelementsbyclassnamegetelementsbytagname

vba Excel getElementsByTagName("a") work but IE.Document.getElementsByClassName("textoblanco") don't work


I use

Dim IE As InternetExplorerMedium
Set IE = New InternetExplorerMedium

 For Each ele In IE.Document.getElementsByTagName("a")
        Debug.Print ele.innerhtml
       If InStr(ele.innerhtml, "Resolver") > 0 Then Debug.Print "OK": Exit For
 next
 For Each ele In IE.Document.getElementsByClassName("textoblanco")
        Debug.Print ele.innerhtml
       If InStr(ele.innerhtml, "Resolver") > 0 Then Debug.Print "OK": Exit For
  next

the wweb page is:

<a href="javascript:botonDuplicarActividad();" class="textoblanco">Duplicar  </a>
<a href="javascript:botonHojaTecnica();" class="textoblanco">Hoja Tec       </a>
<a href="javascript:botonResolver();" class="textoblanco">Resolver       </a>

The first "For each" work and execute Debug.Print "OK" but the second "For Each" not work: Run time :438. object doesn't support this property or methode

i try use a web page from excel


Solution

  • There's no problem with getElementsByClassName in IE unless you have a really old version.

    This works fine for me:

    Sub Tester()
        
        Dim IE As InternetExplorerMedium, ele As Object
        Set IE = New InternetExplorerMedium
        
        IE.Navigate "about:blank"
        IE.Document.Body.innerhtml = _
            "<a href=""javascript:botonDuplicarActividad();"" class=""textoblanco"">Duplicar</a>" & _
            "<a href=""javascript:botonHojaTecnica();"" class=""textoblanco"">Hoja Tec</a>" & _
            "<a href=""javascript:botonResolver();"" class=""textoblanco"">Resolver</a>"
        
        Debug.Print "By Tag Name"
        For Each ele In IE.Document.getElementsByTagName("a")
            Debug.Print , ele.innerhtml
            If InStr(ele.innerhtml, "Resolver") > 0 Then Debug.Print , "OK": Exit For
        Next
        
        Debug.Print "By Class Name"
        For Each ele In IE.Document.getElementsByClassName("textoblanco")
            Debug.Print , ele.innerhtml
            If InStr(ele.innerhtml, "Resolver") > 0 Then Debug.Print , "OK": Exit For
         Next
    End Sub