Skip to content

In VBA, How can we get data shown up with “Inspect Element”, but not with “View Page Source”?

I am trying to scrape a web page which includes multiple tabs. I want to get the quarterly data which is displayed when clicking on By-Quarter Tab, but my code keeps returning yearly data shown when clicking By-Year Tab. The problem is both types of data are on the same URL and when right-clicking “Inspect Element”, their IDs are also the same; you cannot distinguish the quarterly data element ID from yearly data data element ID. “Inspect Element” shows up both quarterlyand yealy data, but “View Page Source” shows up only yealy ones. Could anyone show me how to get the quarterly data please? Thank you very much.

   Sub Getquarterdata()

    Dim html As HTMLDocument
    Set html = New HTMLDocument
    URL = ""
    With CreateObject("MSXML2.XMLHTTP")
        .Open "GET", URL, False
        .SetRequestHeader "User-Agent", "Mozilla/5.0"
        html.body.innerHTML = .responseText

    End With

        ' By "Inspect Element" pointing at Quarterly Data, I counted "td" and came with these lines of code, but they print yearly data.
          Debug.Print html.getElementById("divHoSoCongTyAjax").getElementsByTagName("td")(23).innerText  '=> Print  9,091,070,000 (Year 2017 data)
          Debug.Print html.getElementById("divHoSoCongTyAjax").getElementsByTagName("td")(24).innerText  '=> Print 14,605,578,000 (Year 2018 data)
          Debug.Print html.getElementById("divHoSoCongTyAjax").getElementsByTagName("td")(25).innerText  '=> Print 18,510,898,000 (Year 2019 data)
          Debug.Print html.getElementById("divHoSoCongTyAjax").getElementsByTagName("td")(26).innerText  '=> Print 18,451,311,000 (Year 2020 data)
         ' The thing is that Quarterly Data shows up only with "Inspect Element", but not with "View Page Source"
    Set html = Nothing

End Sub


  1. URL:

  2. Quaterly Data shown when clicking By-Quarter Tab

  3. Yearly Data shown when clicking By-Year Tab

  4. Inspect Quaterly Data:

  5. Inspect yearly Data:



One of the clues given is in the class where you see it says Ajax. This is dynamically added content. If you use the network tab of dev tools (F12), and manually select the quarterly tab, you will see the following request endpoint, which serves the data you are after:

Option Explicit

Public Sub GetQuarterlyTable()
    'required VBE (Alt+F11) > Tools > References > Microsoft HTML Object Library ;  Microsoft XML, v6 (your version may vary)

    Dim hTable As MSHTML.HTMLTable
    Dim xhr As MSXML2.XMLHTTP60, html As MSHTML.HTMLDocument
    Set xhr = New MSXML2.XMLHTTP60
    Set html = New MSHTML.HTMLDocument

    With xhr
        .Open "GET", "", False
        html.body.innerHTML = .responseText
    End With

    Set hTable = html.querySelector(".tab1child_content")
    'Do something with table
End Sub
9 People found this is helpful