5

Mechanize with 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にあるものよりも優れた Mechanize ドキュメント?

ありがとう!

編集: 関連する 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 のバグであることが判明しました。0.6.0v以降を使用していることを確認してください。

于 2013-05-14T17:18:55.403 に答える