Dan
Dan

Reputation: 39

GetElementsByClassName Unable to Get Class InnerText

I can't figure out how to getelementsbyclassname. I have the following Tag. I can do getelementsbytagname by doing ("div") and listing its item number but I wanted to get to the class name item 2 in this instance. On the webpage here is the tag.

<div class="column">

Projected Delivery Date:

</div>


<div class="column">

11/28/2017                                                                                                           
</div>

I am trying to get the date there. This is code that I used to accomplish this. It isn't working with "partner_info group" nor "column". I loop through all the tags and it doesn't seem to pick it up.

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

If anyone can explain how to fix my code so it will pick up the date? Again I could do getelementsbytagname for "div" but I want it by the class name if possible. I am using VBA in Excel to do this. This is not Java Script

Upvotes: 0

Views: 2408

Answers (2)

QHarr
QHarr

Reputation: 84465

Not sure why you can loop all the element with class of column. When you find the one with Projected Delivery Date as value then pick the item at the next index. I use a CSS class selector to get a nodeList of all the matched elements and loop it.

Dim nodeList As Object, i As Long
If htmlDoc Is Nothing Then Exit Sub
Set nodeList =htmlDoc.querySelectorAll(".column")
For i = 0 To nodeList.Length - 1
    If InStr(nodeList.item(i).innerText, "Projected Delivery Date") > 0 Then
        Debug.Print nodeList.item(i + 1).innerText
        Exit For
    End If
Next

Upvotes: 1

garbb
garbb

Reputation: 679

Well I got this working and it reads the innerText of the elements with class="column", so see what you are doing differently and change it until you can get it to work. Also you must add "Microsoft HTML Object Library" to the project under tools>references.

Sub sub1()
Dim hdoc As HTMLDocument

' your test HTML
sHtml = "<div class=""column"">" & vbCrLf & _
vbCrLf & _
"Projected Delivery Date:" & vbCrLf & _
vbCrLf & _
"</div>" & vbCrLf & _
vbCrLf & _
"<div class=""column"">" & vbCrLf & _
vbCrLf & _
"11/28/2017" & vbCrLf & _
"</div>"

Set hdoc = New HTMLDocument
hdoc.body.innerHTML = sHtml

For Each element In hdoc.getElementsByClassName("column")
    Debug.Print element.innerText
Next
End Sub

Upvotes: 0

Related Questions