9

i am building a crawler and i am using Mechanize. I wish to click on a radio button. How do i do that ?

Like for example there are two radio buttons say 'A' and 'B'. The website automatically selects B, but i want 'A' using Mechanize in ruby. I am also using the latest version on Mechanize.

Kaushik Thirthappa
  • 1,041
  • 2
  • 9
  • 21

2 Answers2

15

There are a couple of ways to do this. Probably the best would be to use the radio button's name or id:

form.radiobutton_with(:name => /b/).check

You could also do something like this:

form.radiobuttons.first.check

Which is more succinct, but more likely to break (if for instance you were to change the design of your form).

Alex Peattie
  • 26,633
  • 5
  • 50
  • 52
  • thanks a lot for that. I used `form.radiobuttons[0].checked=true` but that didnt make `form.radiobuttons[1].checked=false` i had to do that manually, anything you can suggest ? – Kaushik Thirthappa Nov 12 '11 at 03:00
0

If you want to access a specific radio button out of a group of radio buttons, you can do so like this:

form.radiobutton_with(name: 'Choose wisely', value: 'Carpenter Goblet').check

This will allow you to choose the specific radio button with the desired value, this is better than selecting the radio button from its group using an index.