Reputation: 132
So here is the current XPath i'm trying to use.
//*[@id="highcharts-33"]/svg/g[3]/g[1]/text
The problem is that this is a Dynamic Xpath, so it goes like "highcharts-33", "highcharts-34" etc... I'm making screenshots of theses graphs by clicking on a checkbox. Each time the checkbox is checked/unchecked the next graphs will have a different ID.
Here is the error i'm getting in the cmd :
NoSuchElementException: Message: Unable to locate element: {"method":"xpath","selector":"//*[contains(@id,'highcharts-')]/svg/g[3]/g[1]/text"}
You can see here i'm trying to use "contains". I also tried to use "starts-with" but i'm still unable to locate the graph.
Here are the things i've tried :
textvote = self.driver.find_element_by_xpath("//*[contains(@id,'highcharts-')]/svg/g[3]/g[1]/text").text
textvote = self.driver.find_element_by_xpath("//*[starts-with(@id,'highcharts-')]/svg/g[3]/g[1]/text").text
I searched on stack overflow and saw some topics on it and it was always starts-with/contains or an unanswered topic. So i may be using starts-with/contains wrong.
If you want me to share more code, don't hesitate. Thank you.
Edit : Adding relevent HTML
<div id="chartListContainer">
<div id="chart0" class="gauge-chart" data-highcharts-chart="16"><div class="highcharts-container" id="highcharts-33"
<div class="highcharts-container" id="highcharts-33"
<svg version="1.1"
<g class="highcharts-data-labels highcharts-tracker"
<g zIndex="1"
<text x="0" zIndex="1" style="font-size:30px;font-family:Open Sans;color:#1abb1a;fill:#1abb1a;" y="32"><tspan>80.0% (4 votes)</tspan></text>
I'm trying to extract the text from that last line.
Upvotes: 0
Views: 1051
Reputation: 2033
I'm assuming that there's only one <text>
element inside your <div id="highcharts-*>
element so you could try CSS selector div[id^="highcharts"] text
textvote = self.driver.find_element_by_css("div[id^='highcharts-'] text").text
This CSS selects all <text>
elements inside a <div>
element with id starting with "highcharts-", but if you have more than one <text>
elements you might run into troubles.
EDIT:
After another look seems that I was wrong to assume that there was only one <text>
element there, a fixed CSS selector to mimic your xpath one is:
textvote = self.driver.find_element_by_css("div[id^='highcharts-']>svg>g:nth-of-type(3)>g:nth-of-type(1)>text").text
Upvotes: 1