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
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.
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.