Selenium/java-xpath for xlink:href attribute

Viewed 5189

I tried to locate use element with attribute value '#aaa' for below snippet

<svg>
    <use xlink:href='#aaa'></use>
</svg>

I was able to locate using css selector use[* |href='#aaa'] . But I require it in xpath. I tried below xpath but it is not working

(//*[name()='use'])[ * ='#aaa' or @href='#aaa']

Note :I need to use the value '#aaa' to differentiate from other elements.

Could anyone please help to construct this xpath.

7 Answers

lookout, if someone needs contains..

"//*[name()='use'][contains(@*='#aaa')]"

as none of the suggestions really worked for me, I came up with following (tested in chrome devtools):

//*[name()='use' and @*[contains(.,'#aaa)]]

comments:

  • name()='use'
    • because a normal tag search (i.e //use) did not work
  • @*[contains(.,'#aaa')]
    • because contains with a wildcard (i.e. contains(@*,'#aaa')) didn't work

For XPath:

//*[name()='use'][contains(@*, '#aaa')]

Or variant provided by Andersson

Approaches with [@xlink:href='#aaa'] don't work on practice

Related