phantomjs - 在 SlimerJS 中处理下载对话框

标签 phantomjs slimerjs

我写了一个脚本,点击一个可以下载 mp3 文件的链接。我面临的问题是当脚本模拟点击该链接时,会弹出一个下载对话框,如下所示:
Download Dialog Box

现在,我想将此文件保存到我选择的某个路径并自动执行整个过程。我对如何处理这个对话框一无所知。

最佳答案

这是改编自 this blog post 的脚本下载文件。

In SlimerJS it is possible to use response.body inside the onResourceReceived handler. However to prevent using too much memory it does not get anything by default. You have to first set page.captureContent to say what you want. You assign an array of regexes to page.captureContent to say which files to receive. The regex is applied to the mime-type. In the example code below I use /.*/ to mean "get everything". Using [/^image/.+$/] should just get images, etc.

var fs=require('fs');
var page = require('webpage').create();

fs.makeTree('contents');

page.captureContent = [ /.*/ ];

page.onResourceReceived = function(response) {

    if(response.stage!="end" || !response.bodySize)
    {
        return;
    }

    var matches = response.url.match(/[/]([^/]+)$/);
    var fname = "contents/"+matches[1];

    console.log("Saving "+response.bodySize+" bytes to "+fname);
    fs.write(fname,response.body);

    phantom.exit();
};

page.onResourceRequested = function(requestData, networkRequest) {
    //console.log('Request (#' + requestData.id + '): ' + JSON.stringify(requestData));
};

page.open("http://....mp3", function(){

});

关于phantomjs - 在 SlimerJS 中处理下载对话框,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35433334/

相关文章:

node.js - 如何在 phantom 模块中设置用户代理字符串?

javascript - 返回只读内容 <input type=text>

phantomjs - 如何将 google web 字体与 phantomjs 一起使用

javascript - 使用 Requests 库从 PhanomJS 服务器获取响应

javascript - CasperJS : dropdown list; select an option, 代码在浏览器和 slimer 中有效,但在 phantom 中无效

ssl - 将 CasperJS 与 "--ssl-protocol=tlsv1 --engine=slimerjs"一起使用时的未知选项

java - 如何在java中使用selenium一次获取多个iframe内容

javascript - 如何使用 slimerJS 允许访问浏览器地理定位 API?

javascript - 重新运行 casperjs 脚本

windows - 如何在 Windows 上使用 CasperJS 设置 SlimerJS?