1

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:

<li>
<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/SelectedDetailsRadio1ドル$" 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">
<strong>
<span id="moneyAmount">1076</span>
NOK
</strong>
Flex
</label>
</li>
Twaldigas
1,65415 silver badges22 bronze badges
asked Dec 8, 2014 at 8:24
1
  • 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')] Commented Dec 9, 2014 at 7:08

3 Answers 3

1

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.

answered Feb 10, 2015 at 22:43
0

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.

answered Dec 8, 2014 at 16:12
0

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.

answered Dec 15, 2014 at 22:09

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.