Selenium cannot find SVG element in XPath
38,022
Solution 1
Try the following XPath expression:
//*[local-name() = 'svg']
(works at least from Chrome/FireBug console, haven't tried with Selenium yet)
Solution 2
The question is about xPath, but if you can use CSS Selectors, that would be more readable, like so (Java).
WebElement image = driver.findElement(By.cssSelector("#imageholder > svg > g > image"));
Related videos on Youtube
Author by
Andrew Daniel
Front-end engineer for Multiplay - part of Unity. I build applications in React, NodeJS, GraphQL (apollo) although I've worked with AngularJS, Angular. I use React-testing library, Jest, and all sorts of JS stuff.
Updated on July 09, 2022Comments
-
Andrew Daniel almost 2 years
I have the following HTML:
<div id="imageholder> <svg> <g> <image href='blah.gif'> </g> </svg> </div>
And I cannot seem to locate the svg with selenium IDE on firefox at all. I have tried:
//svg //svg:svg //*[name()='svg'] //*[namespace-uri()='http://www.w3.org/2000/svg']
None of them can locate my svg element. Sometimes I get the error:
error = TypeError: e.scrollIntoView is not a function
I'm using this as a means to use the locator in JUnit 4 testing if that helps.
-
Len over 10 yearsThanks, this solved a similar issue for me. Any idea why simply '//svg' would not work?
-
Apollon over 10 yearsRelated to XML namespaces if I have understood correctly. (Haven't digged much into this)
-
Mauricio Morales over 8 yearsI do want to clarify that the error
TypeError: e.scrollIntoView is not a function
is merely an error because the SVG DOM element doesn't support scrolling into view thru that function (and thus, the error still happen with @Touko's fix). But that error should be dismissable as it's just Selenium-IDE's way of showing you that it found the element. AwaitForElementPresent
command would work just the same without theTypeError
. -
dansalmo over 6 yearsThis worked to find the element, but failed to find child elements by extending the path. I had first find the svg, then do an addition
svg.find_element
-
John about 6 yearsI would like to know why? CSS works in my Firefox where XPATH does not. But I only have XPATH in LXML for Python, so am doomed to give up.