scrollBy到达网页底部时如何退出循环?
How to exit loop when scrollBy reaches the bottom of the web page?
我在 VBA 中使用 IE 编写了一个脚本来自动到达网页底部。该网页以这样一种方式显示其内容:如果我向下滚动,就会看到更多产品。我在我的脚本中使用 .scrollBy
来处理延迟加载。
我不明白当没有更多新产品要加载时如何停止滚动 - 我在 Do
循环中使用了 .scrollBy
。当滚动完成并且浏览器到达网页底部时,如何退出循环?提前感谢任何解决方案。
这是我目前尝试过的方法:
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
尝试以下我使用等级确定循环的 termination/exit 的地方。
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 选择器的更多信息
下面的选择器针对 class 名称为 rank
并且在其中具有兄弟元素 dt
和 dd
的所有元素。
HTML.querySelectorAll(".rank dt ~ dd")(item)
目标 HTML:
我在 VBA 中使用 IE 编写了一个脚本来自动到达网页底部。该网页以这样一种方式显示其内容:如果我向下滚动,就会看到更多产品。我在我的脚本中使用 .scrollBy
来处理延迟加载。
我不明白当没有更多新产品要加载时如何停止滚动 - 我在 Do
循环中使用了 .scrollBy
。当滚动完成并且浏览器到达网页底部时,如何退出循环?提前感谢任何解决方案。
这是我目前尝试过的方法:
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
尝试以下我使用等级确定循环的 termination/exit 的地方。
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 选择器的更多信息
下面的选择器针对 class 名称为 rank
并且在其中具有兄弟元素 dt
和 dd
的所有元素。
HTML.querySelectorAll(".rank dt ~ dd")(item)
目标 HTML: