-1

I am uable to getText from Text tag in SVG. I am Attaching HTML

<svg width="553" height="200" style="overflow: hidden;" aria-label="A chart.">
     <defs id="defs">
     <rect x="0" y="0" width="553" height="200" stroke="none" stroke-width="0" fill="#ffffff">
     <g>
         <text text-anchor="start" x="77" y="22.85" font-family="Arial" font-size="11" font-weight="bold" stroke="none" stroke-width="0" fill="#000000">Clustering done on Mar 30, 2017 11:13 AM</text>
         <rect x="77" y="13.5" width="400" height="11" stroke="none" stroke-width="0" fill-opacity="0" fill="#ffffff">
     </g>

The get I want to get is Clustering done on Mar 30, 2017 11:13 AM

Ahmed Ashour
  • 4,462
  • 10
  • 33
  • 49
niazi
  • 91
  • 1
  • 9
  • 1
    Please, post the code where you're trying to get text – Ivan Pronin Mar 30 '17 at 08:03
  • 1
    possible duplicate of http://stackoverflow.com/questions/31520642/how-to-use-xpath-in-selenium-webdriver-to-grab-svg-elements – Japu_D_Cret Mar 30 '17 at 08:03
  • 'String text=driver.findElement(By.xpath(".//[@id='divClusters']/div[1]/div[1]/div/svg/g[1]/text")).getText();' System.out.print(text) – niazi Mar 30 '17 at 08:14
  • this is error message**org.openqa.selenium.NoSuchElementException: no such element: Unable to locate element** @japu-d-cret – niazi Mar 30 '17 at 08:17
  • 2
    Possible duplicate of [How to use xPath in Selenium WebDriver to grab SVG elements?](http://stackoverflow.com/questions/31520642/how-to-use-xpath-in-selenium-webdriver-to-grab-svg-elements) – hg8 Mar 30 '17 at 09:08

3 Answers3

0
driver.findElement(By.xpath("//*[local-name='svg']//*[local-name='g']//*[local-name='text']")).getText();
-1

This locator should work:

  • String text=driver.findElement(By.xpath("//[@id='divClusters']//*[name()='svg' and @aria-label='A chart.']//*[name()='g']")).getText(‌​); System.out.print(text);

Explanation:

  • .//div - remove . to search the whole document, not just in currently selected context element
  • remove absolute /div[1]/.. divs reference since it's not reliable
  • refer to svg element as //*[name()='svg']
  • add some meaningful attribute @aria-label='A chart.' to svg element
  • Elements inside svg should be handled in the same way: //*[name()="g"], //*[name()="defs"] - thanks to Andersson for clarifying this.
Ivan Pronin
  • 1,692
  • 15
  • 12
-1

Try below:

driver.findElement(By.cssSelector("#divClusters svg > g‌ > text")).getText();
Kushal Bhalaik
  • 3,250
  • 4
  • 19
  • 38