VBA: How to wait for page to load

一个人想着一个人 提交于 2021-02-05 08:24:28

问题


I'm in the middle of writing a script for signing into a form, and filling in a bunch of "sign up" information and clicking submit.

Thus far, i have managed to get the script to navigate to the sign in page, enter username, enter password and click "sign in".

However, whilst I would normally use the "While IE.readyState <> 4 Or IE.Busy: DoEvents: Wend" function to wait for the next page to load, i am having trouble because unfortunately what appears to be happening is that once the sign in button is clicked, the URL is redirected to about 2 interim "loading" pages/URLs before arriving at the "enter details" page/URL.

This results in a run-time 13 error, as the script thinks the page has already loaded, despite the browser then moving on to the final/correct page.

Here is the code in question:

While IE.readyState <> 4 Or IE.Busy: DoEvents: Wend

    Set UserN = IE.document.getElementByID("login_username")
    UserN.Value = "exampleUser"

    Set PassW = IE.document.getElementByID("login_entered_password")
    PassW.Value = "ExamplePass"

    Set AllHyperLinks = IE.document.getElementsByClassName("label-content")
     For Each hyper_link In AllHyperLinks
         If hyper_link.innerHTML = "Sign in" Then
             hyper_link.Click
             Exit For
         End If
     Next

    While IE.readyState <> 4 Or IE.Busy: DoEvents: Wend
    Set Fname = IE.document.getElementByID("firstname")
    Fname.Value = "John"

End Sub

Essentially the script loads the page>enters username>enters password>clicks sign in>is supposed to wait until the final url is loaded>enters first name in the first name field (after this is working i will write the remainder of the script for entering details.

Any and all help would be amazing! thanks all


回答1:


If IE.ReadyState and IE.Busy aren't working, you could just test for an element you know will exist after the page has loaded and add the Sleep API so you aren't rapid-fire testing Fname until it's not nothing.

Sleep API is

Declare Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As Long)

Then change your While-Wend to

Do Until Not Fname Is Nothing
    Set Fname = IE.document.getElementByID("firstname")
    Sleep 1000 '<- make this however long you want it to wait in ms
Loop



回答2:


I much prefer adding a timed loop testing for presence of an element as this allows you to Exit earlier is element found and is thus potentially more efficient than a hard coded sleep time which will always be completed.

Dim t As Date, ele As Object
Const MAX_WAIT_SEC As Long = 10 '<==Adjust wait time

While ie.Busy Or ie.readyState < 4: DoEvents: Wend
t = timer
Do 
    DoEvents
    On Error Resume Next
    Set ele = IE.document.getElementByID("firstname")
    If Timer - t > MAX_WAIT_SEC Then Exit Do
    On Error GoTo 0
Loop While ele Is Nothing

If Not ele Is Nothing Then
    'do something 
End If


来源:https://stackoverflow.com/questions/54227585/vba-how-to-wait-for-page-to-load

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!