Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Xpath "ends-with" does not work

I am trying to find an input element with dynamic id name always ending with "register". So far I tried this

"//input[@id[ends-with(.,'register')]]" 

and this

"//input[ends-with(@id,'register')]" 

none of these result in an element. What am I doing wrong? At the same time this works:

"//input[@id[contains(.,'register')]]" 

Here's the part of source:

<td class="input"> <input id="m.f0.menu.f2.volumeTabs.BLOCK_COMMON.tcw.form.register" name="m.f0.menu.f2.volumeTabs.BLOCK_COMMON.tcw.form.register" class="aranea-checkbox" type="checkbox"> </td> 
like image 627
casper Avatar asked Mar 16 '14 12:03

casper


People also ask

How do you write an XPath with an end?

XPath Starts-with. XPath Ends-with. Using “OR” Statement. Using “AND” Statement.

Which of the following ends-with same as XPath ends-with?

This is the equivalent to the XPath ends-with. Therefore, $= is the correct answer.

What is text () in XPath?

XPath text() function is a built-in function of the Selenium web driver that locates items based on their text. It aids in the identification of certain text elements as well as the location of those components within a set of text nodes. The elements that need to be found should be in string format.

What version of XPath does chrome use?

No, Chrome uses XPath 1.0. Trying any other XPath 2.0 function such as current-date() will yield a similar error.


Video Answer


2 Answers

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' 
like image 105
Ian Roberts Avatar answered Sep 26 '22 23:09

Ian Roberts


The accepted answer by Ian Roberts uses the @id attribute twice in his solution.

In this case I prefer to put the predicate on that @id like this:

//input[@id[substring(.,string-length(.) - string-length('register') + 1) = 'register']] 
like image 30
Siebe Jongebloed Avatar answered Sep 25 '22 23:09

Siebe Jongebloed