【问题标题】:SelectList with Mechanize in Ruby在 Ruby 中使用机械化选择列表
【发布时间】:2012-04-10 04:18:33
【问题描述】:

我正在尝试使用 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>

【问题讨论】:

  • report_form.field_with(:name =&gt; "report_type").value = "Foo" 应该适用于我的理解。唯一要检查的是查看实际网页。
  • 我也无法在 HTML 中选择任何

标签: ruby mechanize mechanize-ruby


【解决方案1】:

试试这个

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

【讨论】:

  • 你在看.query_value吗?
【解决方案2】:

这实际上是 Mechanize gem 中的一个错误。确保您使用的是 v 0.6.0 或更新版本。

【讨论】:

    【解决方案3】:

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

    【讨论】:

      【解决方案4】:

      我遇到了同样的问题,对我来说也没什么用,但我想澄清一下,除了选择选项之外,我还可以将值设置为任何东西。

          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"]]
      

      【讨论】:

      • 您解决过这个问题吗?我遇到了完全相同的问题,并将
      【解决方案5】:

      这样做通常就足够了:

      report_form["report_type"] = "Foo"
      

      【讨论】:

        猜你喜欢
        • 2012-05-16
        • 2012-07-03
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-01-12
        • 1970-01-01
        • 2015-05-07
        • 1970-01-01
        相关资源
        最近更新 更多