Excel VBA对象与IE自动化所需的错误

所以我有一些代码去一个网站,然后login。 然后我需要它去到密码更改url。 有时当我运行代码时,出现以下错误:

运行时错误424:对象必需

debugging器有时会说这是第一个getelementbyid语句,但大多数时候它说这个问题是最后三个getelementbyid语句。 代码如下:

Dim ie As Variant Dim strURL As String Sub login() Set ie = New SHDocVw.InternetExplorer ie.Visible = True ie.navigate "https://minecraft.net/profile" While ie.Busy DoEvents Wend ie.document.getElementById("username").Value = "ddd" ie.document.getElementById("password").Value = "ddddddddddd" Dim htmlForm As HTMLFormElement Set htmlForm = ie.document.getElementById("loginForm") htmlForm.submit ' ********************************************************************** 'IE.Document.getElementById("username").Value = "ddddd" ' IE.Document.getElementById("password").Value = "ddddd" ' IE.Document.getElementById("signin").Click '********************************************************************** 'Pause while page loads Application.Wait (Now + #12:00:03 AM#) ie.navigate "https://minecraft.net/profile/password" ie.document.getElementById("oldPassword").Value = "oldpass" ie.document.getElementById("password").Value = "enwapss" ie.document.getElementById("passwordConfirmation").Value = "enwapss" Set htmlForm = ie.document.getElementById("loginForm") htmlForm.submit End Sub 

提前致谢!

可能是网站没有处于就绪状态,因为网站在尝试input值时尚未完全加载。

ie.navigate“ https://minecraft.net/profile/password

尝试添加

做直到不ie.Busy和ie.readyState = 4
的DoEvents
循环

这将循环,直到网页加载类似,你在上面的代码与你做的方式

Application.Wait(现在+#12:00:03 AM#)