使用VBA控制IE提交时用户名和密码丢失问题求助
Fix for Username/Password Missing Issue When Automating Playup Login with VBA
Hey there, let's break down why your username and password keep disappearing when automating the Playup login with VBA. The core problem here is not waiting properly for the login form page to load after clicking "Sign In"—you’re trying to fill fields before the page is fully ready, which leads to the values not sticking or getting reset. Let’s fix this step by step.
Key Issues in Your Original Code
- After clicking the "Sign In" link, you immediately attempt to access the username/password fields without waiting for the login form page to finish loading.
- Relying only on
ie.readyState = 4isn’t always reliable—IE might report this state while still busy rendering elements or loading resources.
Modified Working Code
Sub clicksignupurl() Dim ie As Object Dim url_name As String Dim allhyperlinks As Object, hyper_link As Object Dim mailid As Object, passid As Object Set ie = CreateObject("internetexplorer.application") ie.Visible = True url_name = Sheet1.Range("E4").Value ' Handle empty URL case If url_name = "" Then MsgBox "URL is empty in Sheet1 E4!", vbExclamation ie.Quit Set ie = Nothing Exit Sub End If ' Navigate to main site and wait for full load ie.navigate url_name WaitForIE ie ' Locate and click the "Sign In" span Set allhyperlinks = ie.document.getElementsByTagName("span") For Each hyper_link In allhyperlinks If Trim(hyper_link.innerText) = "Sign In" Then hyper_link.Click Exit For End If Next hyper_link ' Critical: Wait for login form page to load after clicking Sign In WaitForIE ie ' Extra safeguard: Wait until username field exists in the DOM Do Until Not ie.document.getElementById("username") Is Nothing DoEvents Loop ' Fill login credentials Set mailid = ie.document.getElementById("username") mailid.Value = "ajmsanvesh143@gmail.com" Set passid = ie.document.getElementById("password") passid.Value = "ajms6237" ' Locate and click the "Sign in" button Set allhyperlinks = ie.document.getElementsByTagName("button") For Each hyper_link In allhyperlinks If Trim(hyper_link.innerText) = "Sign in" Then hyper_link.Click Exit For End If Next hyper_link ' Optional: Wait for post-login page to load if needed WaitForIE ie ' Cleanup (uncomment if you want IE to close automatically) ' ie.Quit ' Set ie = Nothing End Sub ' Helper function to reliably wait for IE to finish loading Sub WaitForIE(ie As Object) Do While ie.Busy Or ie.readyState <> 4 DoEvents Loop End Sub
What Changed & Why
WaitForIEHelper Function: Checks bothie.Busyandie.readyState = 4—this is far more reliable than a single readyState check, as it ensures IE isn’t still processing resources.- Post-Click Wait: After clicking "Sign In", we call
WaitForIEagain to let the login form page fully load before interacting with fields. - Element Presence Check: Added a loop to wait specifically for the username field to exist, preventing attempts to interact with elements that haven’t rendered yet.
- Whitespace Handling: Used
Trim()on innerText checks to avoid issues with hidden spaces in the element’s text. - Basic Error Handling: Added a check for empty URLs to avoid unnecessary navigation attempts.
内容的提问来源于stack exchange,提问作者ANVESH ADDANKI
相关产品推荐
相关产品推荐

