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

huangapple go评论61阅读模式
英文:

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

问题

我使用

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 web page is:

Duplicar
Hoja Tec
Resolver

第一个"For Each"有效并执行 Debug.Print "OK",但第二个"For Each"不起作用:运行时错误:438。对象不支持此属性或方法

我尝试从Excel中使用网页
英文:

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

答案1

得分: 0

以下是您要翻译的代码部分:

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();"">Duplicar</a>" & _
        "<a href=""javascript:botonHojaTecnica();"">Hoja Tec</a>" & _
        "<a href=""javascript:botonResolver();"">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

希望这有助于您的需要。

英文:

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 &quot;about:blank&quot;
    IE.Document.Body.innerhtml = _
        &quot;&lt;a href=&quot;&quot;javascript:botonDuplicarActividad();&quot;&quot; class=&quot;&quot;textoblanco&quot;&quot;&gt;Duplicar&lt;/a&gt;&quot; &amp; _
        &quot;&lt;a href=&quot;&quot;javascript:botonHojaTecnica();&quot;&quot; class=&quot;&quot;textoblanco&quot;&quot;&gt;Hoja Tec&lt;/a&gt;&quot; &amp; _
        &quot;&lt;a href=&quot;&quot;javascript:botonResolver();&quot;&quot; class=&quot;&quot;textoblanco&quot;&quot;&gt;Resolver&lt;/a&gt;&quot;
    
    Debug.Print &quot;By Tag Name&quot;
    For Each ele In IE.Document.getElementsByTagName(&quot;a&quot;)
        Debug.Print , ele.innerhtml
        If InStr(ele.innerhtml, &quot;Resolver&quot;) &gt; 0 Then Debug.Print , &quot;OK&quot;: Exit For
    Next
    
    Debug.Print &quot;By Class Name&quot;
    For Each ele In IE.Document.getElementsByClassName(&quot;textoblanco&quot;)
        Debug.Print , ele.innerhtml
        If InStr(ele.innerhtml, &quot;Resolver&quot;) &gt; 0 Then Debug.Print , &quot;OK&quot;: Exit For
     Next
End Sub

huangapple
  • 本文由 发表于 2023年4月19日 19:30:20
  • 转载请务必保留本文链接:https://go.coder-hub.com/76053980.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定