Wait For Window To Reload When Scrolling Web Page In Vba
Solution 1:
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
DoUntil elemRect.bottom > 0
currPage.parentWindow.scrollBy 0, 10000Set 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.
Solution 2:
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 = -1Set objIE = New InternetExplorer
objIE.navigate fullUrl
DoWhile objIE.Busy = TrueOr objIE.readyState <> 4: DoEvents: LoopSet currPage = objIE.document
DoUntil 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 > 400Then newNum = 400Loop
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)
Post a Comment for "Wait For Window To Reload When Scrolling Web Page In Vba"