本文是小编为大家收集整理的关于在SlimerJS中处理下载对话框的处理/解决方法,可以参考本文帮助大家快速定位并解决问题,中文翻译不准确的可切换到English标签页查看源文。
问题描述
我编写了一个单击可以下载MP3文件的链接的脚本.我面临的问题是,当脚本模拟该链接的单击时,下载对话框像这样弹出:
下载对话框
现在,我想将此文件保存到我选择的某个路径中,并自动化整个过程.我对如何处理此对话框一无所知.
推荐答案
问题描述
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.
推荐答案
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(){ });
其他推荐答案
You can't control a dialog box. SlimerJS doesn't have API for this action.
其他推荐答案
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