关于vba:scrollBy到达网页底部时如何退出循环?

How to exit loop when scrollBy reaches the bottom of the web page?

我已经使用IE在VBA中编写了一个脚本,以自动到达网页的底部。 网页以这样的方式显示其内容:如果向下滚动,则可以看到更多产品。 我在脚本中使用了.scrollBy来处理延迟加载。

我不明白在没有更多新产品要加载时如何停止滚动-我在Do循环中使用了.scrollBy。 滚动完成并且浏览器到达网页底部时,如何退出循环? 在此先感谢您提供任何解决方案。

到目前为止,这是我尝试过的:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
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


请尝试以下使用等级确定循环终止/退出的方法。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
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的所有元素,然后在其中包含同级元素dtdd

1
HTML.querySelectorAll(".rank dt ~ dd")(item)

目标HTML:

HTML element