Home > Back-end >  Excel VBA using Selenium - click on list element to change focus
Excel VBA using Selenium - click on list element to change focus

Time:10-18

I'm trying to scape data from a website via Excel VBA. I have a web page which has different data depending on a button selection, but the button sits withing a ul list. I can find the element by class using:

.FindElementByClass("shared-filter-button-list_navItem__ZiG2J")

But I can seem to work out how to switch the focus between 'This season' and 'All time' to change to displayed data on the page. Any ideas would be gratefully received. The html is:

<ul ><li ><button  value="This season" type="button">This season</button></li><li ><button  value="All time" type="button">All time</button></li></ul>

CodePudding user response:

It would help to see the page, but if you just want to click the "this season" or "all time" button, just find the buttons inside the list you already have and click one?

update I misread the provided HTML (its all in one line) and thought that shared-filter-button-list_navItem__ZiG2J was the container ul not the list items, and also that selenium uses 1-based indexes not 0-based.

The code below finds all buttons that match the query and prints their index and text to the debug window.

Private Driver As Selenium.ChromeDriver

Sub Main()
Set Driver = New Selenium.ChromeDriver

Driver.Get "https://www.euroleaguebasketball.net/eurocup/players/lukas-meisner/011187/"

Dim List As Selenium.WebElements
' get a list of button that are children of li with specified class name
Set List = Driver.FindElementsByXPath("//li[contains(@class, 'shared-filter-button-list_navItem__ZiG2J')]/button", 0, 5000)

If List Is Nothing Then
    Debug.Print "failed to get list"
    Exit Sub
End If

For Index = 1 To List.Count
    Debug.Print Index & " -> " & List(Index).Text
Next Index

End Sub

Expected result from this code is:

1 -> This season
2 -> All time
3 -> Regular Season

If you wanted to click the This season button

List(1).Click
  • Related