sprogissd
sprogissd

Reputation: 3075

How to grab correct highchart number in selectors using Selenium Webdriver with Python with several charts on the page?

I am testing a web application that uses highcharts. The selectors look like this, and the highchart number of the same chart is always different. For example:

#highcharts-4 >div:nth-child(1) > span > div > span

When there is only one chart on a page, I do the following, and it works perfectly:

[id^='highcharts-'] > div:nth-child(1) > span > div > span

It selects the first element where id begins with the string 'highcharts-', but how can I select the second and the third elements if let's say I have several charts present on the same page?

For example, when there are three identical charts, the same element on each chart would have the following selectors with ID being different by two all the time:

#highcharts-4 >div:nth-child(1) > span > div > span
#highcharts-6 >div:nth-child(1) > span > div > span
#highcharts-8 >div:nth-child(1) > span > div > span

How can I grab the second and the third ones?

Upvotes: 2

Views: 306

Answers (1)

Breaks Software
Breaks Software

Reputation: 1761

You should be able to use the method to select multiple elements that match your selector:

my_charts = driver.find_elements_by_css_selector("[id^='highcharts-'] > div:nth-child(1) > span > div > span")
for chart in my_charts:
    print chart.text

(you didn't mention what you were doing with those charts, but here I'm just printing whatever text might be associated with it)

Upvotes: 2

Related Questions