我不知道如何通过类名获取元素。我有以下标签。我可以通过执行 ("div") 并列出其项目编号来执行 getelementsbytagname,但我想在此实例中获取类名 item 2。在网页上,这里是标签。
<div class="column">
Projected Delivery Date:
</div>
<div class="column">
11/28/2017
</div>
我正试图在那里找到日期。这是我用来完成此操作的代码。它不适用于“partner_info group”或“column”。我遍历所有标签,但似乎没有捡起它。
Private Function TrackNEW(trackingNumber As String) As String
Dim xml As Object
Dim tempString As String
Dim htmlDoc As Object ' MSHTML.HTMLDocument
Dim htmlBody As Object ' MSHTML.htmlBody
Dim anchors As Object ' MSHTML.IHTMLElementCollection
Dim anchor As Object ' MSHTML.IHTMLElement
Dim dda As Object ' MSHTML.IHTMLElementCollection
Dim ddb As Object
Dim ddc As Object
Dim ddd As Object
Dim span As Object
Dim div As Object ' MSHTML.IHTMLElement
Set xml = GetMSXML
If xml Is Nothing Then ' cannot start MSXML 6.0
TrackNEW = MSXML_ERROR
Exit Function
End If
tempString = GetResponse(xml, HTTP_GET, NEWUrl & trackingNumber, False)
If Len(tempString) = 0 Then
TrackNEW = ERROR_MSG
Exit Function
End If
Set htmlDoc = CreateHTMLDoc
If htmlDoc Is Nothing Then ' cannot reference MSHTML object library
TrackNEW = MSHTML_ERROR
Exit Function
End If
Set htmlBody = htmlDoc.body
htmlBody.innerHtml = tempString
On Error Resume Next
Set dda = htmlDoc.getElementsByTagName("span")
Set ddb = htmlDoc.getElementsByTagName("span")
Set ddc = htmlDoc.getElementsByTagName("span")
Set ddd = htmlDoc.getElementsByClassName("column")
For Each Strg4 In ddd
For ItemNumber4 = 1 To 600
Strg4 = ddd.Item(ItemNumber4).innerText
If InStr(Strg4, "Projected Delivery Date:") >= 1 Then
Strg4 = ddd.Item(ItemNumber4).innerText
GoTo Line8
Else
End If
Next ItemNumber4
Next Strg4
MsgBox "Bad"
Exit Function
Line8:
TrackNEW = Strg4
Exit Function
如果有人可以解释如何修复我的代码,以便它会选择日期?我可以再次为“div”做getelementsbytagname,但如果可能的话,我希望它通过类名。我在 Excel 中使用 VBA 来执行此操作。这不是 Java 脚本