0

I am trying to detect the number of the total pages .. so as to deal with all the pages and here's my attempt

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

After the first page it goes to the next page but got an error after that as for the ele variable

1
  • 1
    What is the error message? Commented Jun 30, 2020 at 11:01

1 Answer 1

1

I would use a different approach and get the number of pages from

Dim numPages As Long
numPages = bot.FindElementsByCss("[href^='./']").count

Then do a loop from For i = 2 to numPages, and simply

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

I would avoid using your ele being set outside of loop as element may become stale after click event.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.