Xpath "ends-with" does not work

XpathEnds With

Xpath Problem Overview


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>

Xpath Solutions


Solution 1 - Xpath

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'

Solution 2 - Xpath

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

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestioncasperView Question on Stackoverflow
Solution 1 - XpathIan RobertsView Answer on Stackoverflow
Solution 2 - XpathSiebe JongebloedView Answer on Stackoverflow