Forum: Ruby Need to parse a list of checkboxes - ruby mechanize

0faae51834740c2142efb5af4b8d8175?d=identicon&s=25 Rochit Sen (rochitsen)
on 2013-03-23 18:01
Hi All,

I have a ruby mechanize form that has a list of some 45 checkboxes. Now
i want to parse that list and then check each checkbox. Any idea how? As
i donot want to write a line of code for each checkbox, i would want to
parse the checkboxes list.
I can get the name of a checkbox using the following code:

chkName = checkboxForm.checkbox.name

Now how would i parse the whole list to store the names in an array.
Once i get those in the array i can use the following code in a for loop
to compare the name in the mechanize checkbox_with(:name =>
'checkbox_name').check

Please let me know what other better approach we can use.

regards.
0faae51834740c2142efb5af4b8d8175?d=identicon&s=25 Rochit Sen (rochitsen)
on 2013-03-23 18:47
Rochit Sen wrote in post #1102943:
> Hi All,
>
> I have a ruby mechanize form that has a list of some 45 checkboxes. Now
> i want to parse that list and then check each checkbox. Any idea how? As
> i donot want to write a line of code for each checkbox, i would want to
> parse the checkboxes list.
> I can get the name of a checkbox using the following code:
>
> chkName = checkboxForm.checkbox.name
>
> Now how would i parse the whole list to store the names in an array.
> Once i get those in the array i can use the following code in a for loop
> to compare the name in the mechanize checkbox_with(:name =>
> 'checkbox_name').check
>
> Please let me know what other better approach we can use.
>
> regards.

Hi All,

Was able to resolve it by spending some more thought on it :)). Did the
following:
------
#Store all the checkbox names in a names array and compare the checkbox
name to check it
allChkBoxes = checkboxForm.checkboxes
chkNames = []
allChkBoxes.each do |chkBx|
  chkNames = chkBx.name
  checkboxForm.checkbox_with(:name => "#{chkNames}").check
end
------
Aa082c8b00a50928e5860dcd70bf2368?d=identicon&s=25 tamouse mailing lists (Guest)
on 2013-03-23 18:56
(Received via mailing list)
On Sat, Mar 23, 2013 at 12:47 PM, Rochit Sen <lists@ruby-forum.com>
wrote:
>>
>
> end
> ------
>
> --
> Posted via http://www.ruby-forum.com/.
>

It looks to me like you're trying to set all the checkboxes in a form,
the above seems to be going about it the hard way. Try:

    checkboxForm.checkboxes.map{|b| b.click}
14b5582046b4e7b24ab69b7886a35868?d=identicon&s=25 Joel Pearson (virtuoso)
on 2013-03-23 19:51
tamouse's suggestion is probably the best way to do this. As you already
have the objects in a collection it's as simple as iterating through it
and performing the same action on each.
If you also want the names then you could do this:

checkboxForm.checkboxes.map{|b| b.click; b.name }

It will click each checkbox and return an array of the names as well.
Please log in before posting. Registration is free and takes only a minute.
Existing account

NEW: Do you have a Google/GoogleMail, Yahoo or Facebook account? No registration required!
Log in with Google account | Log in with Yahoo account | Log in with Facebook account
No account? Register here.