2013-05-11 53 views
5

我试图使用机械化来模拟点击网页上的一个按钮,然后将在浏览器中启动文件下载。这是我的代码如何保存通过按钮点击机械化下载的文件

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) 

从页按钮的输出被如此显示,这意味着它的右按钮的代码段:

#<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(形式,按钮) “我怎样才能让Mechanize检索点击该按钮时将被发送到浏览器的文件?

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

顺便说一句我是一个完全新手红宝石和机械化,所以对不起,如果这是一个愚蠢的问题,但谢谢任何回应!

L:

回答

5

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

response = agent.submit(form, button) 
File.open('saved_file', 'wb'){|f| f << response.body} 
+1

好极了,那在做正是我需要的,谢谢pguardiario! – user2373168 2013-05-12 07:55:38