Unable to locate element because of a dynamic xpath
<p>So here is the current XPath i'm trying to use.</p>

<pre><code>//*[@id="highcharts-33"]/svg/g[3]/g[1]/text
</code></pre>

<p>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. </p>

<p>Here is the error i'm getting in the cmd :</p>

<blockquote>
  <p>NoSuchElementException: Message: Unable to locate element: {"method":"xpath","selector":"//*[contains(@id,'highcharts-')]/svg/g[3]/g[1]/text"}</p>
</blockquote>

<p>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.</p>

<p>Here are the things i've tried :</p>

<pre><code>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
</code></pre>

<p>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. </p>

<p>If you want me to share more code, don't hesitate.
Thank you.</p>

<p>Edit : Adding relevent HTML</p>

<pre><code>&lt;div id="chartListContainer"&gt;
&lt;div id="chart0" class="gauge-chart" data-highcharts-chart="16"&gt;&lt;div      class="highcharts-container" id="highcharts-33"
&lt;div class="highcharts-container" id="highcharts-33"
&lt;svg version="1.1"
&lt;g class="highcharts-data-labels highcharts-tracker"
&lt;g zIndex="1"
&lt;text x="0" zIndex="1" style="font-size:30px;font-family:Open Sans;color:#1abb1a;fill:#1abb1a;" y="32"&gt;&lt;tspan&gt;80.0% (4 votes)&lt;/tspan&gt;&lt;/text&gt;
</code></pre>

<p>I'm trying to extract the text from that last line.</p>

<p>I'm assuming that there's only one <code>&lt;text&gt;</code> element inside your <code>&lt;div id="highcharts-*&gt;</code> element so you could try CSS selector <code>div[id^="highcharts"] text</code></p>

<pre><code>textvote = self.driver.find_element_by_css("div[id^='highcharts-'] text").text
</code></pre>

<p>This CSS selects all <code>&lt;text&gt;</code> elements inside a <code>&lt;div&gt;</code> element with id starting with "highcharts-", but if you have more than one <code>&lt;text&gt;</code> elements you might run into troubles.</p>

<p>EDIT:</p>

<p>After another look seems that I was wrong to assume that there was only one <code>&lt;text&gt;</code> element there, a fixed CSS selector to mimic your xpath one is:</p>

<pre><code>textvote = self.driver.find_element_by_css("div[id^='highcharts-']&gt;svg&gt;g:nth-of-type(3)&gt;g:nth-of-type(1)&gt;text").text
</code></pre>

python xpath selenium-webdriver