seleniumsvgxpath

Selenium/java-xpath for xlink:href attribute


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.


Solution

  • 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']"