VBA Internet Explorer wait for web page to load

后端 未结 3 531
星月不相逢
星月不相逢 2020-12-03 18:38

I know questions like this have been asked before, but mine is a bit different and has been fairly troubling. What I\'m dealing with is a web page with a form with a few ev

相关标签:
3条回答
  • 2020-12-03 19:25

    After an exhaustive search, I've determined that the AJAX request, javascript code that runs asynchronously in the background isn't something I can get a signal from in any way. It does seem to trigger some event when it finishes with loading the page, which is an option I'd like to explore in the future. However, for my purposes I simply used the same code I was already using to fill out the form on my page and I have it loop through each field again to check to see if the values are still correct before clicking the submit button. It isn't an ideal solution, but it is a workaround that appears to have taken care of my issue in this case.

    I'm not sure if my solution would be applicable to someone else dealing with this issue, but there it is if it helps. I think workarounds for this issue are going to have to be based on the application and web page in question.

    0 讨论(0)
  • 2020-12-03 19:33

    I had the same problem with My Webpage.. What is did is...

    the fist option is

    While Ie.**document**.readystate="complete"
    DoEvents
    Wend
    

    there were few boxes in which options were loaded after a button click/even fire in another box...I jsst placed code like this..

    Do Until IE.document.getelementbyid("Next box").Lenght>0
    DoEvents
    Loop
    Application.wait Now+Timevalue("00:00:02)
    
    0 讨论(0)
  • 2020-12-03 19:35

    Old question I know, but I think this is a good answer that I haven't seen about much...


    I had a similar problem; waiting for all the images on a google image search to load (which is a tricky thing since image loads are prompted by AJAX and the user scrolling the page). As has been suggested in the comments; my solution was to wait for a certain element to appear in the viewport (this is an area a little larger than the monitor screen, and is treated as what you can actually "see").

    This is achieved with the getBoundingClientRect method

    Dim myDiv As HTMLDivElement: Set myDiv = currPage.getElementById("fbar") 
    'myDiv should some element in the page which will only be visible once everything else is loaded
    Dim elemRect As IHTMLRect: Set elemRect = myDiv.getBoundingClientRect
    Do Until elemRect.bottom > 0 'If the element is not in the viewport, then this returns 0
        DoEvents
        'Now run the code that triggers the Ajax requests
        'For me that was simply scrolling down by a big number
        Set elemRect = myDiv.getBoundingClientRect
    Loop
    myDiv.ScrollIntoView
    

    I explain in detail in the linked answer how this works, but essentially the BoundingClientRect.bottom is equal to 0 until the element is in the vieport.

    The element is something which is loaded straight away (like a frame/template for the page). But you don't actually see it until all the content has been loaded, because it's right at the bottom.

    If that element is indeed the last thing to be loaded (for my google search it was the Show More Results button), then as long as you get it into the viewport when it's loaded, you should be able to detect when it appears on the page. .bottom then returns a non-zero value (something to do with the actual position of the element on the page - I didn't really care though for my purposes). I finish off with a .ScrollIntoView, but that's not essential.

    0 讨论(0)
提交回复
热议问题