Wait for window to reload when scrolling web page in VBA

前端 未结 2 1436
后悔当初
后悔当初 2021-01-26 11:54

I have written a VBA macro to count the (approximate) number of images returned for a Google search of a specific term. By approximate I mean that the program should count the n

相关标签:
2条回答
  • 2021-01-26 12:21

    Just do this instead, I am sure you can find a nicer way to do it (if you think it's worth the time) but this should be fine :

    newNum = -1
    Set objIE = New InternetExplorer
    objIE.navigate fullUrl
    Do While objIE.Busy = True Or objIE.readyState <> 4: DoEvents: Loop
    Set currPage = objIE.document
    Do Until oldNum = newNum
        oldNum = newNum
        newNum = currPage.getElementById("rg_s").getElementsByClassName("rg_di rg_bx rg_el ivg-i").Length        
        Application.Wait Now + TimeSerial(0, 0, 2)
        currPage.parentWindow.scrollBy 0, 100000        
        Application.Wait Now + TimeSerial(0, 0, 2)
        If newNum > 400 Then newNum = 400
    Loop
    

    Then you just have to adapt the delay in TimeSerial depending on how fast your computer loads ( in here I set in to 2 seconds)

    0 讨论(0)
  • 2021-01-26 12:26

    Through further research I've come up with this approach:

    Dim myDiv As HTMLDivElement: Set myDiv = currPage.getElementById("fbar")
    Dim elemRect As IHTMLRect: Set elemRect = myDiv.getBoundingClientRect
    Do Until elemRect.bottom > 0
        currPage.parentWindow.scrollBy 0, 10000
        Set elemRect = myDiv.getBoundingClientRect
    Loop
    myDiv.ScrollIntoView
    

    Where currPage is the HTML webpage (Dim currPage As HTMLDocument) and myDiv is a particular element. The type is not important, but it should be noted that myDiv is always located at the bottom of the document and is only loaded once everything else has been. So for Google images that's the help bar, which you only get to after scrolling through all the image results.

    How it works

    The code works as follows: myDiv.getBoundingClientRect is a way of checking whether an element is visible in the browser - that's why we need to look at an element at the bottom of the page, as if we scroll until that becomes visible, then everything else must have loaded too.

    That's of course where the Do Until...Loop comes from; we loop until the elemRect.bottom value is not zero (as when the element is not in view, it's zero, once it's in view it becomes a non-zero number). More info on that see here

    Finally, use a myDiv.ScrollIntoView to get the browser right to the bottom; this is necessary because the BoundingClientRect is visible slightly before the element is on screen, so we need to scroll the last bit in order to load the final images.

    Why not just use ScrollIntoView form the start? It doesn't work, since the element hasn't loaded yet.

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