在Mechanize中为selectlist设置值

时间:2012-07-03 04:54:49

标签: ruby mechanize mechanize-ruby

考虑以下Mechanize表单对象

#<Mechanize::Form
 {name "f1"}
 {method "POST"}
 {action "f.php"}
 {fields
  [hidden:0x4db4b02 type: hidden name: opflag value: ]
  [text:0x4db463e type: text name: lno value: 666]
  [selectlist:0x4db84dc type:  name: scode value: []]}
 {radiobuttons}
 {checkboxes}
 {file_uploads}
 {buttons [button:0x4db42ec type: button name: bt value:  Show Result ]}>

在这里,我可以使用

设置文本字段的值
result_form = page.form('f1')
result_form.lno = '666'

但是我在为scode选择列表设置值方面遇到了困难。我试过了

result_form.field_with(name:"scode").option_with(value: "foo").click

它返回一个ERROR undefined method点击'for nil:NilClass(NoMethodError)` 然后,我试过

result_form.scode.value = 'foo'

但这也会返回NoMethodError。知道如何在Mechanize中为selectlist设置值吗?

2 个答案:

答案 0 :(得分:2)

尝试:

result_form.scode = 'foo'

答案 1 :(得分:1)

result_form["scode"] = "foo"应该做到这一点。 Reference in the docs

相关问题