我已经使用 IE 在 VBA 中编写了一个脚本来自动到达网页的底部。网页以这样的方式显示它的内容,如果我向下滚动更多的产品变得可见。我.scrollBy
在我的脚本中使用来处理延迟负载。
我不明白如何在没有更多新产品加载时停止滚动 - 我.scrollBy
在Do
循环中使用过。当滚动完成并且浏览器到达网页底部时,如何退出循环?提前感谢您提供任何解决方案。
这是我迄今为止尝试过的:
Sub HandleLazyload()
Const URL As String = "https://www.inc.com/profile/sumup-payments-limited"
Dim IE As New InternetExplorer, HTML As HTMLDocument, post As Object
With IE
.Visible = True
.navigate URL
While .Busy = True Or .readyState < 4: DoEvents: Wend
Set HTML = .document
End With
Do
HTML.parentWindow.scrollBy 0, 99999
Application.Wait Now + TimeValue("00:00:03")
Set post = HTML.getElementsByTagName("article")
Loop ''I wish to break out of this loop when all the scrolling is done
IE.Quit
End Sub
尝试以下我使用排名来确定循环的终止/退出的地方。
Option Explicit
Public Sub HandleLazyload()
Const URL As String = "https://www.inc.com/profile/sumup-payments-limited"
Dim IE As New InternetExplorer, HTML As HTMLDocument
With IE
.Visible = True
.navigate URL
While .Busy = True Or .readyState < 4: DoEvents: Wend
Set HTML = .document
End With
Dim rank As Long, item As Long
item = 1
Do While Err.Number = 0
HTML.parentWindow.scrollBy 0, 99999
Application.Wait Now + TimeSerial(0, 0, 1)
On Error GoTo errhand
rank = Split(HTML.querySelectorAll(".rank dt ~ dd")(item).innerText, "#")(1)
item = item + 1
Loop
errhand:
Err.Clear
Debug.Print "Stopped at rank " & rank
'Your other code
'IE.Quit
End Sub
笔记:
CSS 选择器:
如果您想了解有关 CSS 选择器的更多信息
下方目标的所有元素,其中的类名是选择rank
,然后有兄弟元素dt
和dd
内。
HTML.querySelectorAll(".rank dt ~ dd")(item)
目标 HTML:
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句