简体   繁体   中英

Excel VBA object required error with IE automation

So I have some code to go to a website and then login. Then I need it to go to the password change URL. Sometimes when I run the code, I get the following error:

Runtime Error 424: Object Required

Sometimes the debugger says it is the first getelementbyid statement, but most times it says the issue is the last three getelementbyid statements. The code is below:

 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

Thanks in advance!

It may be that the website isn't in a ready state, as in the site hasn't fully loaded when it's attempting to input the values.

After

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

Try adding

Do Until Not ie.Busy And ie.readyState = 4
DoEvents
Loop

This will loop until the webpage has loaded similar, to the way you've done it in your above code with

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM