How to select a radio button using Mechanize in Ruby?

I create a crawler and I use Mechanize. I want to press the switch. How to do it?

For example, there are two switches: "A" and "B". The website automatically selects B, but I want "A" using Mechanize in ruby. I also use the latest version of Mechanize.

+6
source share
2 answers

There are several ways to do this. It is probably best to use the name or id radio button:

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

You can also do something like this:

 form.radiobuttons.first.check 

This is more eloquent, but is likely to break (if, for example, you change the design of your form).

+13
source

If you want to access a specific switch from a group of switches, you can do this as follows:

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

This will allow you to select a specific switch with the desired value, this is better than choosing a radio button from your group using the index.

0
source

Source: https://habr.com/ru/post/901288/


All Articles