document.getElementsByTagName(“tr”)。当我执行VBA脚本以查找特定网页上的tr元素数时,length返回零
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
答案 0 :(得分:2)
在您的网站上找不到tr标签可能是由于在页面完全加载之前执行宏而导致的。尝试更改:
Do While IE.readyState < 4
为:
Do While IE.readyState < 4 or IE.Busy
即使这并不保证在触发宏之前所有内容都会加载,但通常会有所帮助,并且始终包含它是一种很好的做法。有时您需要找到显示&#34; loading&#34;在前端和循环遍历HTML元素,直到它消失 - 然后你可以安全地运行你的脚本。