ktkaushik ktkaushik - 11 months ago 78
Ruby Question

How to select a Radio Button using Mechanize in Ruby?

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.

Answer Source

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:


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