使用Ruby中的Mechanize选择List

DNa*_*del 5 ruby mechanize mechanize-ruby

我正在尝试使用Mechanize with Ruby设置选择列表的值.我可以使用选择列表导航到页面,使用.form方法获取表单,然后找到选择列表.

report_form =page.form('form1')
pp report_form.field_with(:name => "report_type")
Run Code Online (Sandbox Code Playgroud)

正确返回正确的对象.

但是,我仍然无法设置此字段的值!我试过了:

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"
Run Code Online (Sandbox Code Playgroud)

但是当我这样做时:

pp report_form.field_with(:name => "report_type")
Run Code Online (Sandbox Code Playgroud)

值字段仍为空.

有什么我想念的吗?提示?窍门?比在http://mechanize.rubyforge.org上生活的更好的机械化文档?

谢谢!

编辑:相关的HTML是:相关的HTML是:

<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>
Run Code Online (Sandbox Code Playgroud)

Joh*_*hat 7

试试这个

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

(通过1,2)