Need to parse a list of checkboxes - ruby mechanize

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.

Rochit S. 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

On Sat, Mar 23, 2013 at 12:47 PM, Rochit S. [email protected]
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}

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.