在Excel VBA中使用getElementsByClassName

3
以下是我使用的代码,但出现了以下错误:
对象不支持此属性或方法
在使用 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

3
不确定你想要做什么,使用 "new2",但是在 getElementsByClassName() 中的 "element" 后面缺少一个 "s"。 - Porcupine911
1
我能想到两件事情:1)您使用的IE版本不支持该方法,2)在您点击链接元素后,您需要等待页面加载。 - David Zemens
1
不要将 Set new2 = ... 赋值给 .innerText 属性。只需将其分配给字符串类型变量。str = ie....innertext(并声明您的变量!) - user4039065
在HTML页面中有这个条目<p class="highlighted_message">No tasks have been defined.</p>,我正在尝试检查该文本是否存在于HTML页面中,如果是,则进行进一步的工作。我已经尝试在getElementsByClassName()中包含缺失的s,因为我在复制时犯了一个错误,并且已经等待页面加载完成。 - Aravind Scorpion
您正在错误地使用 Instr 函数 - 应该是 InStr(ie.Document.Body.outerText, "No tasks have been defined.")。正如 florentbr 在下面指出的那样,如果您无法提供可访问的 URL,则很难提供任何有用的帮助。 - Tim Williams
显示剩余3条评论
1个回答

3
要获取一个类名为的元素,我会使用querySelector:
Set element = ie.Document.querySelector(".classname")
Debug.Print element.innerHTML

使用你的示例:

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

我在使用querySelector时仍然遇到相同的错误:( - Aravind Scorpion
你能提供准确的错误信息吗?我很确定你至少会在使用querySelector时遇到另一个错误。 - Florent B.
它的“运行时错误438:”对象不支持此属性或方法。与ClassName方法所显示的内容相同。 - Aravind Scorpion

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接