1

我正在使用 mechanize 处理表单。我已经使用 mechanize 解析了表单,输出如下:

{forms
 #<Mechanize::Form
 {name nil}
 {method "POST"}
 {action "/dashboard/checks/50114dbeae6f61b428000ad8"}
 {fields
  [hidden:0x60c476a type: hidden name: _method value: put]
  [text:0x60c4616 type: text name: check[name] value: Testing]
  [text:0x60c4512 type: text name: check[url] value: http://www.pintile.com]
  [text:0x60c445e type: text name: check[interval] value: 120]
  [text:0x60c435a type: text name: check[maxTime] value: 1500]
  [textarea:0x60c4116 type:  name: check[tags] value: ]}
 {radiobuttons}
 {checkboxes}
 {file_uploads}
 {buttons
  [button:0x60c3d88 type: submit name:  value: ]
  [button:0x60c3d24 type: submit name: delete value: ]

此表单中有 2 个按钮 保存更改(第 1 个)、删除(第 2 个)、 我使用以下代码来保存更改,它工作正常:

form.field_with(:name => "check[name]").value = "Testing"
button = form.buttons.first
agent.submit(form, button)

更改已成功保存。但是当我尝试使用下面的代码删除它不起作用:

button = form.buttons.first
agent.submit(form, button)

它什么也不做。请帮我解决这个问题。

4

4 回答 4

0

如果网站是典型的 Rails 表单,则删除按钮很可能是 JavaScript 操作。机械化不支持 JavaScript。你可能想使用像 capybara 这样的东西和 web kit 驱动程序,它有完整的 JavaScript 支持和你在 mechanize 中已经拥有的所有功能。

于 2012-07-27T08:56:19.783 回答
0

The code snippet below work well with link.Have you tried click

agent.get("http://your url")

agent.page.link_with(:text => "link name").click

Give it a try.. Else as mentioned above you can use Capybara

于 2012-07-27T10:04:13.120 回答
0

Delete is the last button. Therefore you want:

button = form.buttons.last
agent.submit(form, button)

or more simply:

form.submit form.buttons.last
于 2012-11-17T00:20:26.627 回答
0

If Mechanize is good enough for you and you do not require any javascript support that would bring Capybara, you should be able to emulate what the Javascript does. In this case, the source would be needed to better know what append, but I guess the value of the hidden field _method is replaced by delete prior the submit.

于 2013-05-30T09:30:42.680 回答