Dan
Dan

Reputation: 39

Get Element By ? tag name id or class name How do you get the innertext of below

The UPS Website changed. Instead of the tag it change to

Private Function TrackUPS(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 dds As Object  ' MSHTML.IHTMLElementCollection
    Dim ddr As Object
    Dim dt As Object
    Dim dd As Object  ' MSHTML.IHTMLElement

    'tempString = GetMSXMLWebResponse(UPSUrl & trackingNumber)

    Set xml = GetMSXML
    If xml Is Nothing Then  ' cannot start MSXML 6.0
        TrackUPS = MSXML_ERROR
        Exit Function
    End If

    tempString = GetResponse(xml, HTTP_GET, UPSUrl & trackingNumber, False)

    If Len(tempString) = 0 Then
        TrackUPS = ERROR_MSG
        Exit Function
    End If

    Set htmlDoc = CreateHTMLDoc
    If htmlDoc Is Nothing Then  ' cannot reference MSHTML object library
        TrackUPS = MSHTML_ERROR
        Exit Function
    End If

    Set htmlBody = htmlDoc.body
    htmlBody.innerHTML = tempString
    On Error Resume Next
    Set dds = htmlDoc.getElementsByclassname("").innerText
    'Set dds = htmlDoc.getElementsByTagName("dd")
    Set ddr = htmlDoc.getElementsByTagName("dt")
    Strg1 = htmlDoc.getElementById("tt_spStatus").innerText
    Strg2 = dds.Item(1).innerText
    Strg3 = dds.Item(11).innerText
    Strg4 = htmlDoc.getElementById("tt_pgfStatus").innerText
    Strg5 = htmlDoc.getElementById("tt_ovntStatus").innerText
    If Len(Strg1) = 0 Then
        Strg1 = Strg4
        If Len(Strg4) = 0 Then
            Strg1 = Strg5
        End If
    End If
    PODEnd10 = Strg1 & "|" & Strg2
    If PODEnd10 = "|1.   " Then GoTo Line1 Else GoTo Line2
    Line2:
    If PODEnd10 = "|>>>1." Then GoTo Line1 Else GoTo Line3
    Line3:
    TrackUPS = Strg1 & "|" & Strg2
    Exit Function
    Line1:
    TrackUPS = "NO|POD|INFO"
    Exit Function
End Function

So the website shows the Day and Time with this:

<p class="">
    Monday, &nbsp;01/11/2016
    at&nbsp;9:09 A.M.
</p>

I use this in the above code: Set dds = htmlDoc.getElementsByclassname("").innerText However the <p class=""> has no name. How do I capture the innertext of that element? Any help would be greatly appreciated. I rely on this daily.

Here is the entire div class:

<div class="ups-group ups-group_condensed">
p class="ups-form_label"><strong>Delivered On:</strong></p>

<p class="">
    Monday, &nbsp;01/11/2016
    at&nbsp;9:09 A.M.
</p>
</div>

What would be the .innertext coding in vba to return the Monday and the date and time of the P class""?

Upvotes: 0

Views: 2895

Answers (1)

Dan
Dan

Reputation: 39

This worked:

Set dds = htmlDoc.getElementsByTagName("p")
Strg2 = dds.Item(11).innerText

Upvotes: 0

Related Questions