Ram Ch Ram Ch - 1 month ago 8
Java Question

How to locate custom html elements using selenium webdriver

I have the following html code

<svg height="600" width="910" xmlns:xlink="http://www.w3.org/1999/xlink"
id="svgcontent" overflow="visible" x="910" y="600" viewBox="0 0 910 600">
<g style="pointer-events:all">
<text fill="rgb(0,0,0)" font-family="Sans-Serif" font-size="13" id="80-155-126"
transform="rotate(-90, 168, 126)" y="126" x="168" style="pointer-
events:inherit">A100A</text>
</g>
</svg>


I tried in the following way but didnt get succeeded

assertEquals(driver.findElements(By.xpath("//*[local-name()='svg'
and namespace-uri()='http://www.w3.org/2000/svg' and @id='svgcontent']//*[local-
name()='g' and namespace-uri()='http://www.w3.org/2000/svg']/text")).size(), 1);

Answer

try following

assertEquals(driver.findElements(By.xpath("//*[local-name()='svg' and @id='svgcontent']//*[local-name()='text']")).size(), 1); 

it should work, but

assertEquals(driver.findElements(By.xpath("//*[local-name()='svg' and namespace-uri()='http://www.w3.org/2000/svg' and @id='svgcontent']//*[local-name()='g' and namespace-uri()='http://www.w3.org/2000/svg']/*[local-name()='text']")).size(), 1);

should be also ok.