5

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

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

正确返回正确的对象。

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

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"

但是当我这样做时:

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

值字段仍为空。

有什么我想念的吗?尖端?诡计?比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>
4

5 回答 5

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"

(通过12

于 2012-03-26T18:54:02.647 回答
2

通常这样做就足够了:

report_form["report_type"] = "Foo"
于 2012-03-27T01:20:37.100 回答
1

我遇到了同样的问题,对我也没有任何作用,但我想澄清一下,我能够将值设置为除了选择选项之外的任何东西。

    report_form.field_with(:name => "report_type").value = "Foo1"
    report_form["report_type"]
    => "Foo1"
    report_form.field_with(:name => "report_type").value
    => "Foo1"
    report_form.field_with(:name => "report_type")
    => [selectlist:0x7c08ada type:  name: "report_type" value: []]

提交表单后,选择被视为空,但是如果我这样做

    report_form.field_with(:name => "report_type").value = "anything not in the options"
    report_form.field_with(:name => "report_type")
    => [selectlist:0x7c08ada type:  name: "report_type" value: ["anything not in the options"]]
于 2013-02-20T17:49:56.280 回答
0

Foo 不在选择列表中,我认为如果您将其更改为 Foo1 (或其他)它应该可以工作!?

于 2013-03-20T15:57:18.403 回答
0

它实际上是 Mechanize gem 中的一个错误。确保您使用的是 v0.6.0或更新版本。

于 2013-05-14T17:18:55.403 回答