nvkrj
nvkrj

Reputation: 1033

VBA Data Scraping from Websites

document.getElementsByTagName("tr").length returns zero when I execute a VBA script to find the number of tr elements on a specific web page

Sub AutomaticMode()
Set IE = CreateObject("InternetExplorer.Application")
IE.Visible = True
IE.Navigate "https://www.example.com/"
Do While IE.readyState < 4
    Application.StatusBar = "DOM Loading ..."
Loop
Set username_field = IE.document.getElementByID("username")
username_field.Value = "username"
Set password_field = IE.document.getElementByID("password")
password_field.Value = "password"
SendKeys "{Tab}{Enter}"
Do While IE.readyState < 4
    Application.StatusBar = "DOM Loading ..."
Loop
Dim trList As IHTMLElementCollection
Set trList = IE.document.getElementsByTagName("tr")
MsgBox (trList.Length)
End Sub

Upvotes: 0

Views: 1004

Answers (1)

Ryszard Jędraszyk
Ryszard Jędraszyk

Reputation: 2412

No tr tags found on your website can be caused by macro being executed before page fully loads. Try changing:

Do While IE.readyState < 4

to:

Do While IE.readyState < 4 or IE.Busy

Even this doesn't guarantee that everything loads before firing macro, but often helps and is a good practice to always include it. Sometimes you need to find object which displays "loading" etc. in frontend and loop through HTML elements until it disappears - then you are safe to run your script.

Upvotes: 2

Related Questions