Javascript 为什么可以';我不能使用GetElementById在页面上查找元素吗?
您好,我正在使用VBA开发Internet Explorer自动化解决方案。检查我的网页并检查感兴趣的元素以确定其ID后。Javascript 为什么可以';我不能使用GetElementById在页面上查找元素吗?,javascript,html,vba,dom,getelementbyid,Javascript,Html,Vba,Dom,Getelementbyid,您好,我正在使用VBA开发Internet Explorer自动化解决方案。检查我的网页并检查感兴趣的元素以确定其ID后。IE.document.GetElementByID方法失败,因为找不到ID。我应该补充的是,网页是动态的。所以我想我应该等一会儿,让javascripts完成运行。我暂停了VBA代码的执行1分钟,但这没有帮助 我还将.body.innerHTML和.body.outerHTML的内容分配给一个文本文件,以检查内容,但找不到该元素。我还保存了一个完整的网页,并查看了用文本编辑
IE.document.GetElementByID
方法失败,因为找不到ID。我应该补充的是,网页是动态的。所以我想我应该等一会儿,让javascripts完成运行。我暂停了VBA代码的执行1分钟,但这没有帮助
我还将.body.innerHTML
和.body.outerHTML
的内容分配给一个文本文件,以检查内容,但找不到该元素。我还保存了一个完整的网页,并查看了用文本编辑器生成的html文件,但仍然缺少页面上的元素(以及更多元素)
编辑:添加了示例代码
Sub Test_Login()
Dim ie As InternetExplorer
Set ie = New InternetExplorer
ie.Navigate2 "http://www.example.com"
Do
If ie.ReadyState = 4 Then
ie.Visible = True
Exit Do
Else
DoEvents
End If
Loop
Application.Wait Now + TimeSerial(0, 0, 20)
ie.document.getElementById("commandSignIn").Click
End Sub
这就是我用来处理IE项目的方法:
IE.Document.All.Item("InputField")
要等待网站加载,请执行以下操作:
Do Until Not .Busy
' nothing needed here
Loop
从Internet Explorer登录到网站的完整代码
Private Sub LoginToWebsite()
' needs references to
' microsoft internet controls
' microsoft HTML object library
' Prepare to open the web page
Set IE = CreateObject("InternetExplorer.Application")
With IE
.Visible = True
.Navigate "www.WebsiteToLoginTo.com"
' Loop until the page is fully loaded
Do Until Not .Busy
' nothing needed here
Loop
' Make the desired selections on the web page and click the submit Button
' get handle on user name item and enter value
Set webItem = IE.Document.all.Item("UserNameEntry")
webItem.Value = "username"
' get handle on password item and enter value
Set webItem = IE.Document.all.Item("PasswordEntry")
webItem.Value = "password"
' get handle on submit button item and click
Set webItem = IE.Document.all.Item("SubmitButton")
webItem.Value = "submit"
webItem.Click
' Loop until the page is fully loaded
Do Until Not .Busy
' nothing needed here
Loop
End With
End Sub
它是
getElementById
,首字母小写为“g”。是的,谢谢,但VBA不区分大小写,所以这不是问题。您可以通过internetexplorer.application发布准确的代码行吗?你在等readyState吗?等等,你说你检查了主体的实际html内容,但元素不在那里。所以很明显这个方法不会返回任何结果。。。