'What is the XPath to an attribute of an svg Element?

Lets say my website contains multiple svg elements and I want to find the following:

<svg id="some-svg-id" />

How can I find this svg via its id?

I aware that //*[local-name()='svg'] finds all svg elements but how can I narrow it further down to one specific via its id?



Solution 1:[1]

Try this one

//svg[@id='some-svg-id']

or, since id should be unique, this one:

//*[@id='some-svg-id']

Solution 2:[2]

Hi you can use below solution to fix your problem:

//[name()='svg']//[local-name()='g' and @class='your-class-name']

or

//[name()='svg']//[local-name()='g' and @class='your-class-name']//*[text='rect']

Use * after // something like : //*

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Mate Mrše
Solution 2 Shivu B Sasanur