How to select the first element with a specific attribute using XPath

Use: (/bookstore/book[@location=’US’])[1] This will first get the book elements with the location attribute equal to ‘US’. Then it will select the first node from that set. Note the use of parentheses, which are required by some implementations. Note, this is not the same as /bookstore/book[1][@location=’US’] unless the first element also happens to have that location … Read more

Using upper-case and lower-case xpath functions in selenium IDE

upper-case() and lower-case() are XPath 2.0 functions. Chances are your platform supports XPath 1.0 only. Try: translate(‘some text’,’abcdefghijklmnopqrstuvwxyz’,’ABCDEFGHIJKLMNOPQRSTUVWXYZ’) which is the XPath 1.0 way to do it. Unfortunately, this requires knowledge of the alphabet the text uses. For plain English, the above probably works, but if you expect accented characters, make sure you add them … Read more

Xpath “ends-with” does not work

The ends-with function is part of xpath 2.0 but browsers (you indicate you’re testing with chrome) generally only support 1.0. So you’ll have to implement it yourself with a combination of string-length, substring and equals substring(@id, string-length(@id) – string-length(‘register’) +1) = ‘register’

XPath OR operator for different nodes

All title nodes with zipcode or book node as parent: Version 1: //title[parent::zipcode|parent::book] Version 2: //bookstore/book/title|//bookstore/city/zipcode/title Version 3: (results are sorted based on source data rather than the order of book then zipcode) //title[../../../*[book] or ../../../../*[city/zipcode]] or – used within true/false – a Boolean operator in xpath | – a Union operator in xpath that … Read more

Is there an “if -then – else ” statement in XPath?

Yes, there is a way to do it in XPath 1.0: concat( substring($s1, 1, number($condition) * string-length($s1)), substring($s2, 1, number(not($condition)) * string-length($s2)) ) This relies on the concatenation of two mutually exclusive strings, the first one being empty if the condition is false (0 * string-length(…)), the second one being empty if the condition is … Read more

How to select specified node within Xpath node sets by index with Selenium?

This is a FAQ: //someName[3] means: all someName elements in the document, that are the third someName child of their parent — there may be many such elements. What you want is exactly the 3rd someName element: (//someName)[3] Explanation: the [] has a higher precedence (priority) than //. Remember always to put expressions of the … Read more

How can I match on an attribute that contains a certain string?

Here’s an example that finds div elements whose className contains atag: //div[contains(@class, ‘atag’)] Here’s an example that finds div elements whose className contains atag and btag: //div[contains(@class, ‘atag’) and contains(@class ,’btag’)] However, it will also find partial matches like class=”catag bobtag”. If you don’t want partial matches, see bobince’s answer below.