Excel VBA分页检测

Excel VBA分页检测,excel,vba,selenium,Excel,Vba,Selenium,我正在尝试检测总页数。。为了处理所有的页面,下面是我的尝试 Sub Test() Dim bot As New WebDriver, ele As WebElement, sURL As String, x As Long sURL = "https://mercati.ilsole24ore.com/obbligazioni/titoli-di-stato/btp/1" bot.Start "Chrome", sURL b

我正在尝试检测总页数。。为了处理所有的页面,下面是我的尝试

Sub Test()
    Dim bot As New WebDriver, ele As WebElement, sURL As String, x As Long
    sURL = "https://mercati.ilsole24ore.com/obbligazioni/titoli-di-stato/btp/1"
    bot.Start "Chrome", sURL
    bot.Get sURL
    '//*[@id="r_pagingArea"]/div/a[5]
    Set ele = bot.FindElementByXPath("//*[@id='r_pagingArea']/div/a[5]")
    For x = 1 To 10
        If ele.IsDisplayed Then
            Debug.Print "Page " & x
            ele.Click
            bot.ExecuteScript "window.focus();"
        Else
            Exit For
        End If
    Next x
    MsgBox "Total of " & x & " Pages"
End Sub

在第一页之后,它转到下一页,但在那之后出现了一个错误。至于ele变量,我将使用不同的方法,从中获取页数

Dim numPages As Long
numPages = bot.FindElementsByCss("[href^='./']").count
然后从i=2的
循环到numPages
,只需

bot.get "https://mercati.ilsole24ore.com/obbligazioni/titoli-di-stato/btp/" & i

我会避免使用您的
ele
设置在循环之外,因为元素在单击事件后可能会过时。

错误消息是什么?非常感谢您提供了出色的解决方案。