Selenium/java-xpath for xlink:href attribute
Asked Answered
X

8

6

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.

Xenophanes answered 18/9, 2017 at 6:45 Comment(0)
C
8

Your predicate [ * ='#aaa' or @href='#aaa'] returns false as there is no attribute as href (but xlink:href), and use has no child nodes with text content '#aaa'.

Note that wild-card for any attribute is @* while just * is for any node (except attribute). Try below XPath

"//*[name()='use' and @*='#aaa']"
Cralg answered 18/9, 2017 at 8:22 Comment(1)
FYI: This works for selenium in c# as wellSunbeam
E
2

For XPath:

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

Or variant provided by Andersson

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

Eulalia answered 13/9, 2018 at 10:25 Comment(0)
R
2

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
Reannareap answered 31/1, 2020 at 7:43 Comment(0)
T
1

lookout, if someone needs contains..

"//*[name()='use'][contains(@*='#aaa')]"
Teem answered 16/11, 2017 at 6:47 Comment(0)
A
0

This is an SVG tag and it works a little differently

Please try below xpath:

//[local-name()='svg']/yourxpath
Alamo answered 18/9, 2017 at 7:3 Comment(2)
Could you please let me know whether it works or notAlamo
It does not work.Warman
D
0

Use the xpath

//use[@xlink:href='#aaa']

or

//use[contains(@xlink:href,'#aaa')]
Dunleavy answered 18/9, 2017 at 7:52 Comment(1)
both suggestions end up invalid xpath expressionLonesome
H
0

As the element is within svg namespace you can use the following xpath:

//*[name()="svg"]/use[contains(@xlink:href,'#aaa')]
Herzog answered 18/9, 2017 at 7:58 Comment(0)
O
0

Try this who wants contains

//@*[name()='xlink:href' and contains(., '#aaa')]
Olson answered 3/6 at 8:52 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.