2

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?

sprogissd
  • 2,755
  • 5
  • 24
  • 45
  • Possible duplicate of [Selenium WebDriver \[Java\]: How to Click on elements within an SVG using XPath](https://stackoverflow.com/questions/41829000/selenium-webdriver-java-how-to-click-on-elements-within-an-svg-using-xpath) – undetected Selenium Jan 20 '18 at 08:13

1 Answers1

2

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)

Breaks Software
  • 1,721
  • 1
  • 10
  • 15