Handle download dialog box in SlimerJS

我们两清 提交于 2019-12-01 21:51:48

问题


I have written a script that clicks on a link which can download a mp3 file. The problem I am facing is when the script simulates the click on that link, a download dialog box pops up like this:
Download Dialog Box

Now, I want to save this file to some path of my choice and automate this whole process. I am clueless on how to handle this dialog box.


回答1:


Here's a script adapted from this blog post to download a file.

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(){

});



回答2:


You can't control a dialog box. SlimerJS doesn't have API for this action.




回答3:


Firefox generates a temp "downloadfile.extension.part" file which contains the content. Just simply rename the file ex. myfile.csv.part > myfile.csv

locally if working on a mac you should find the .part file in the downloads directory, on linux /temp/ folder

Not the most elegant solution but should do the trick



来源:https://stackoverflow.com/questions/35433334/handle-download-dialog-box-in-slimerjs

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!