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

时间:2016-01-24 15:22:11

标签: excel excel-vba vba

所以我有一些代码去网站然后登录。然后我需要它去密码更改URL。有时当我运行代码时,我收到以下错误:

  

运行时错误424:对象必需

有时调试器说它是第一个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

提前致谢!

1 个答案:

答案 0 :(得分:1)

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

  

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

尝试添加

  

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

这将循环,直到网页加载类似于您在上面的代码中使用

完成的方式
  

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