I have this problem with locating dynamic buttons.

It's a search for ferries, and for example, outbound ferry is selectedDetailsRadio_1_4_1 and inbound is selectedDetailsRadio_1_4_2. These are fine and easy to locate.

However, depending on the date the search can return several departures the same day, or economy/flex/business class selections. Then the buttons change name to to selectedDetailsRadio_1_5_1, ...1_6_1, ...1_7_1, (and for inbound: 1_5_2/1_6_2/1_7_2) and so on. And since selectedDetailsRadio_1_4_1 isn't on the page anymore, the test fail.

So I need to wildcard the number which is dynamic and changes, I think, but I cannot figure out how. I've Googled for this, but all I can find is starts-with and contains for testing, and since I need a single digit in the middle of the id to be wildcarded, I don't think contains will do it.

Can anyone point me in the correct direction?

Edit: Code example:

<input id="selectedDetailsRadio_1_5_2" type="radio" onclick=" enableDisable(1,'2','5');     changeSailing(1,'to 12 mars 2015 08:00','to 12 mars 2015 11:45','2015-03-12T08:00:00'); "     value="1,5,2," title="1i5j2f08:00t" name="Search/FerryInformation/SelectedDetailsRadio$1$"     tabindex="18"/>
<input id="timeFormattedDep_1_5_2" type="hidden" value="to 12 mars 2015 08:00"     tabindex="19"/>
<input id="timeFormattedArr_1_5_2" type="hidden" value="to 12 mars 2015 11:45"     tabindex="20"/>
<input id="milliSecsDep_1_5_2" type="hidden" value="1426143600000" tabindex="21"/>
<input id="milliSecsArr_1_5_2" type="hidden" value="1426157100000" tabindex="22"/>
<label for="outbound-economy1">
<span id="moneyAmount">1076</span>
  • Why does contains not work? The names of the IDs before _1_5_2 etc. are unique, right? A XPath like this should work for the first element and similar XPaths for the others: //input[contains(@id, 'selectedDetailsRadio')]
    – Twaldigas
    Commented Dec 9, 2014 at 7:08

3 Answers 3


I'm using something like this with selenium webdriver and firefox so I'm constrained to XPath 1.0 features:

xpath("//input[starts-with(@id, 'foo_') and ends-with(@id, '_bar')]")

That should in theory catch any id that looks like these that starts-with foo_ and ends-with _bar:

<input id="foo_1_bar"/>
<input id="foo_2_bar"/>
<input id="foo_beer_bar"/>
<input id="foo_and so on and so forth_bar"/>

I've had some trouble making the matches with ends-with in XPath 1.0, so I'd recommend using contains wherever possible. This is however purely anecdotal.

W3C's specs on XPath, XQuery, and XSLT Functions has been very helpful for me.


You can get list of all elements say by type, then loop over them, check the ID, and by some custom rules decide which one to pick. You have quite a mess on your hands, but do what you can.


As Twaldigas said, use contains in an xpath query to find all elements with 'selectedDetailsRadio' in the input id, then iterate through the results. You can analyze the numbers in the id either using string indices or regex.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.