在Excel VBA中使用getElementsByClassName

时间:2016-03-13 19:34:21

标签: excel vba excel-vba

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

  

对象不支持此属性或方法

使用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

1 个答案:

答案 0 :(得分: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