6

我正在尝试使用 Mechanize 来模拟单击网页上的按钮,然后在浏览器中启动文件下载。这是我的代码片段

form = page.forms.first # => Mechanize::Form
form = agent.page.form_with(:name => "aspnetForm")

button = form.button_with(:value => "GPX file")
pp button

agent.submit(form, button)

pp按钮的输出因此显示,这意味着它是正确的按钮:

#<Mechanize::Form::Submit:0x89fe874
 @name="ctl00$ContentBody$btnGPXDL",
 @node=
  #(Element:0x44ff480 {
    name = "input",
    attributes = [
      #(Attr:0x44476d2 { name = "type", value = "submit" }),
      #(Attr:0x44476c8 {
        name = "name",
        value = "ctl00$ContentBody$btnGPXDL"
        }),
      #(Attr:0x44476be { name = "value", value = "GPX file" }),
      #(Attr:0x44476a0 { name = "id", value = "ctl00_ContentBody_btnGPXDL" })]
    }),
 @value="GPX file">

但是发出了“agent.submit(form, button)”,我怎样才能让 Mechanize 检索将在单击该按钮时发送到浏览器的文件?

我已经搜索并找到了获取网页或链接的方法,但没有看到任何适合这种情况的方法?

顺便说一句,我是 ruby​​ 和 Mechanize 的新手,如果这是一个愚蠢的问题,我很抱歉,但感谢您的任何回复!

男:

4

1 回答 1

7

文件或页面应该由提交返回:

response = agent.submit(form, button)
File.open('saved_file', 'wb'){|f| f << response.body}
于 2013-05-11T23:50:44.150 回答