Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SelectList with Mechanize in Ruby

I'm trying to set the value of a select list using Mechanize with Ruby. I can navigate to the page with the select list, grab the form using the .form method, and find the select list.

report_form =page.form('form1')
pp report_form.field_with(:name => "report_type")

Correctly returns the right object.

However, I'm still unable to set the value of this field! I've tried:

report_form.field_with(:name => "report_type").options.first.select
report_form.field_with(:name => "report_type").options[1].select
report_form.field_with(:name => "report_type").value = "Foo"

But when I then do:

pp report_form.field_with(:name => "report_type")

The value field is still empty.

Is there something I'm missing? Tips? Tricks? Better Mechanize docs than what live at http://mechanize.rubyforge.org?

Thanks!

Edit: The relevant HTML is: The relevant HTML is:

<TD>
<select id="report_type" name="report_type">
    <option value="Foo1">Opt 1</option>
    <option value="Foo2">Opt 2</option>
    <option value="Foo3">Opt 3</option>
</select></TD>
like image 470
DNadel Avatar asked Mar 26 '12 16:03

DNadel


Video Answer


1 Answers

Try this

report_form.field_with(:name => "report_type").option_with(:value => "Foo").click
# now report_form.field_With(:name => "report_type").value should bee "Foo"

(via 1, 2)

like image 170
John Douthat Avatar answered Sep 29 '22 17:09

John Douthat