Select first option of multiple radio button groups

Hello all,

I have run into a snag when trying to handle this situation. I have a web page that has 3 different questions that have radio buttons for their answers. Is there a way to only capture and select the first answer in each group? I have my class pulling all radio buttons right now using xpath //input[@type=radio] so I’m wondering if there is something in xpath to only select the first one or if there is a better method of doing so.

I appreciate any and all help with this,

Tyler P.

Hi Tyler,

first of all, you have to identify a group of radio buttons - if there is some ID or find an identifier based on question text. Then, it is easy to select first radio box using WebUI.selectOptionByIndex({{TestObject}}, 0)

Can you share a part of your HTML code? I may help you with it.

Hi Tyler,

Try writing your xpath selector as (//input[@type=radio])[1] .
It has worked for me before.


this won’t work in this case. Tyler wants to check first element of each group, so if you have 3 groups of 3 boxes, he wants to check 1, 4 and 7. For this reason it is necessary to identify each group of boxes and select first one.

Thanks, Marek,

I didn’t see Tyler was asking about each group.

Back to the coffee machine!

Hello again,

I appreciate your suggestions above but I think I have another piece of the puzzle to throw in here. In order to test a number of different pages (roughly 120 different ones at any given time) I need to try and avoid using test objects. I have a method currently that can pull the elements from the driver session so as of now I’m pulling all radio buttons. I can definitely use an xpath selector to gather the groups, but then how do I select the first child within that given group.

Just to be clear - you have 120 pages and you want to select first radio button on each of them, right? Then Mate’s solution seems to be correct.

@Marek Melocik 120 pages, some with multiple groups and some without

Oh, I see. This is kinda tricky as you:
1/ Must know, which pages have multiple groups and which have not
2/ Must be able to identify (somehow) each group on page with multiple groups

First point is not so difficult, you can create Map with url:flag pair (flag can be single or multiple). But for each ‘multiple’ page, you must have identifier for each group, then you can select first option. Looks like dirty solution unless you have the same identifiers across all pages.