0

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
6
  • 1
    What about trList.Count? Commented Mar 30, 2016 at 16:42
  • trList.Count makes the program return an error: "Object doesn't support this property or method" trList is an IHTMLElementCollection if that is of any help Commented Mar 30, 2016 at 16:58
  • If you put a break on the Set trList = ... line and wait a little while before continuing execution, does that change the outcome ? Commented Mar 30, 2016 at 17:35
  • Try declaring trList like this instead: Dim trList As Object, then try using .count Commented Mar 30, 2016 at 17:35
  • i just tested that for 'div' it is returning the correct length - 18 divs total - but for tr it is not and the page is not showing any frames in its source code as well (considering that the tr's may be in a separate frame) Commented Mar 30, 2016 at 17:42

1 Answer 1

2

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.

Sign up to request clarification or add additional context in comments.

3 Comments

Good point, but I would go for Or instead of And just to be extra safe. Still doesn't guarantee anything though.
Thanks! Your answer is correct. Although Do While IE.readyState < 4 and IE.Busy didn't return the results, but the problem was that the 'tr' elements were loading sometime after the page loaded. On putting the script on Wait for 5 seconds before it started looking for tr's, it finally returned them! Thanks again!!
It is true that the statement should be with "or", not "and", my mistake as I got used to condition with "until".

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.