Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to choose a radio button when the radio buttons have the same id but have different values using capybara

Tags:

capybara

Below is the html code for two radio buttons, they only differ by "value" attribute

<input id="AmountOption" name="AmountOption" type="radio" value="1">
<input id="AmountOption" name="AmountOption" type="radio" value="2">

What Im trying to do is to choose the radio button with "value =2 "

I tried using "choose("AmountOption")" which choses the first radio button, but i want to choose the second radio button

How can i achieve this with "choose("")" method provided by capybara.

Any other alternatives are also welcome.

Thanks in Advance

like image 584
Kalyan Avatar asked Dec 06 '25 13:12

Kalyan


1 Answers

Look at implementation of choose:

def choose(locator, options={})
  find(:radio_button, locator, options).set(true)
end

So it's obvious that you should invoke method set on some element.

:radiobutton selector supports only id, name and label so you can't use it and should use some other selector type:

find('#AmountOption[value=2]').set(true) # selector type is :css by default
like image 99
Andrei Botalov Avatar answered Dec 09 '25 17:12

Andrei Botalov



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!