在 Excel VBA 中使用 getElementsByClassName

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/35974796/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 10:17:36  来源:igfitidea点击:

Using getElementsByClassName in Excel VBA

excelvbaexcel-vba

提问by Aravind Scorpion

Below is the code which I'm using but I'm getting this error that:

以下是我正在使用的代码,但出现以下错误:

object doesn't support this property or method

对象不支持此属性或方法

while using getElementsByClassName. The new 2 variable I'm using is not getting filled please help me out, do let me know if I'm doing it the wrong way.

在使用getElementsByClassName. 我正在使用的新 2 变量没有被填满,请帮助我,如果我做错了,请告诉我。

Sub PopulateTasks()
'Variable Declaration
Dim ie As Object
Dim noTaskText As String
Set ie = CreateObject("InternetExplorer.Application")

url = "http://example/do/"
    .Visible = True
    .Navigate url
    .Top = 50
    .Left = 430
    .Height = 400
    .Width = 400
    Do Until Not ie.Busy And ie.readystate = 4
        DoEvents
    Loop
End With
Set link = ie.Document.getElementsByTagName("a")

For i = 1 To 200
        For Each l In link
            If l.innertext = storyIds(i) Then
                l.Click
                Do Until Not ie.Busy And ie.readystate = 4
                    DoEvents
                Loop
                If InStr("No tasks have been defined.", ie.Document.Body.outerText) <> 0 Then
                    noTaskFound = True
                End If
                noTaskText = ie.Document.getElementsByClassName("highlighted_message")(0).innerText

            If noTaskFound = True Then

            End If
        Next
            ie.Document.getElementbyId ("")                
            Do Until Not ie.Busy And ie.readystate = 4
                DoEvents
            Loop     
Next i    
End Sub

回答by Florent B.

To get an element with the class name, I would use querySelector:

要获取具有类名的元素,我将使用 querySelector:

Set element = ie.Document.querySelector(".classname")
Debug.Print element.innerHTML

With your example:

以你的例子:

txt = ie.Document.querySelector(".highlighted_message").innerText
If txt = "No tasks have been defined." Then
    noTaskFound = True
    Exit For
End If