Search code examples
vbaexcelie-automation

IE automation through VBA - wait for element to disappear


I am filling a web form, which has an built-in preloader animation (which does not impact the browser states) in it, and I am trying to make my script wait for it to be gone, and only then proceed with filling the fields.

Currently I am using this code:

 Do
      Set el = Nothing
      On Error Resume Next
      Set el = objIE.document.getElementsByClassName("preloader__loader")(0)
        On Error GoTo 0
        DoEvents
        Sleep 2000
    Loop While Not (el Is Nothing)

    objIE.document.getElementsByName("button_submit")(0).Click

Not sure why, but still, sometimes this code is not working properly and the Submit button (button_submit) is not pressed.

I would like to avoid wait sentences as much as I can, as they are unreliable in times the page is loading slowly.

Are there any better ways for script to wait for a particular elements (in this case "preloader__loader") to disappear?

Thank you in advance.


Solution

  • So, here is what I did, the first part of the code is waiting for Preloader to appear, and when it does, second part is waiting for the Preloader to disappear.

    This solutions seems to be working quite good for now, I hope there would not be any additional issues if the page will be loading slower than expected :)

          Do
            Set el = Nothing
            On Error Resume Next
            Set el = objIE.document.getElementsByClassName("preloader__loader")(0)
            On Error GoTo 0
            DoEvents
            Sleep 500
        Loop While el Is Nothing
    
          Do
            Set el = Nothing
            On Error Resume Next
            Set el = objIE.document.getElementsByClassName("preloader__loader")(0)
            On Error GoTo 0
            DoEvents
            Sleep 500
        Loop While Not (el Is Nothing)