After a long try I’ve been able to create a script in vba which can successfully handle webpages with lazy-load. It can reach the bottom of a slow loading webpage if the hardcoded number of the loop is set accurately. I tried with few such pages and found it working flawlessly. The one I’m pasting below is created using finance.yahoo
site. It can parse the title of different news after going down to a certain level of that page according to the loop I’ve defined. Now, what I wanna expect to have is do the same thing without using hardcoded delay what I’ve already used in my script. Thanks in advance for any guidance to the improvement.
Here is what I’ve written:
Sub Web_Data() Dim IE As New InternetExplorer, html As HTMLDocument Dim storage As Object, posts As Object With IE .Visible = True .navigate "https://finance.yahoo.com/" Do While .readyState <> READYSTATE_COMPLETE: Loop Set html = .document End With Application.Wait Now() + TimeValue("00:00:005") For scroll_down = 1 To 10 Set storage = html.getElementsByClassName("StretchedBox") html.parentWindow.scrollBy 0, 99999 Application.Wait Now() + TimeValue("00:00:005") Next scroll_down For Each posts In storage Row = Row + 1: Cells(Row, 1) = posts.ParentNode.innerText Next posts IE.Quit End Sub
Reference to add to the library:
1. Microsoft Internet Controls 2. Microsoft HTML Object Library