问题
I almost have this working, I just can't seem to download the file when it comes up. What am I doing wrong here? When clicking the button "Download Sales Report" a CSV should download, by my console.log() never even fires off.
var casper = require('casper').create();
casper.start('http://www.waynecountyauditor.org/Reports.aspx?ActiveTab=Sales')
.waitForText("Accept")
.thenClick('#ctl00_ContentPlaceHolder1_btnDisclaimerAccept')
.waitForText("View Sales")
.thenClick('#ctl00_ContentPlaceHolder1_WeeklySales_fvSalesReport_btnViewSales')
.waitForText("Download Sales Report")
.thenClick(x('//*[@id="ctl00_blSearchLinks"]/li[4]/a'))
.wait(1000)
.on('page.resource.received', function(resource) {
console.log('here');
if (resource.stage !== "end") {
return;
}
if (resource.url.indexOf('results.csv') > -1) {
this.download(resource.url, 'D:\Jobs\Currency\testing\ExportData.csv');
}
});
casper.run();
回答1:
I finally figured it out. Vaviloff had 99% of what I needed. I was just missing 2 post variables. Thanks for your help Vaviloff!
// http://stackoverflow.com/questions/33903418/downloading-a-file-with-casperjs-from-post-attachment
var casper = require('casper').create({
verbose: true,
logLevel: 'debug',
pageSettings: {
loadImages: false // The WebPage instance used by Casper will
, loadPlugins: false // use these settings
, userAgent: 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_7_5) AppleWebKit/537.4 (KHTML, like Gecko) Chrome/22.0.1229.94 Safari/537.4'
}
});
var x = require('casper').selectXPath;
var utils = require('utils');
casper.on('remote.message', function(message) {
this.echo('LOG: ' + message);
});
casper.start()
.open('http://clintonoh.ddti.net/Reports.aspx?ActiveTab=Sales')
.waitForText("Accept")
.thenClick('#ctl00_ContentPlaceHolder1_btnDisclaimerAccept')
.waitForText("View Sales")
.thenClick('#ctl00_ContentPlaceHolder1_WeeklySales_fvSalesReport_btnViewSales')
.waitForText("Download Sales Report", function(){
// Adapted from: http://stackoverflow.com/questions/16144252/downloading-a-file-that-comes-as-an-attachment-in-a-post-request-response-in-pha
var res = this.evaluate(function() {
document.getElementById('__EVENTTARGET').value='ctl00$blSearchLinks' /* Was missing these 2 */
document.getElementById('__EVENTARGUMENT').value='4'
var res={};
f=document.getElementById("aspnetForm");
var previous_onsubmit = f.onsubmit;
f.onsubmit = function() {
//previous_onsubmit();
//iterate the form fields
var post={};
for(i=0; i<f.elements.length; i++) {
//console.log(f.elements[i].name + " = " + f.elements[i].value);
post[f.elements[i].name]=f.elements[i].value;
}
res.action = f.action;
res.post = post;
return false; //Stop form submission
}
// Trigger the click on the link.
var link = document.evaluate('//*[@id="ctl00_blSearchLinks"]/li[5]/a', document, null, XPathResult.FIRST_ORDERED_NODE_TYPE, null).singleNodeValue;
try {
var e = document.createEvent('MouseEvents');
e.initMouseEvent('click', true, true, window, 0, 0, 0, 0, 0, false, false, false, false, 0, null);
link.dispatchEvent(e);
} catch(error){
console.log(error);
}
return res; //Return the form data to casper
});
//Start the download
casper.download(res.action, "./ExportData.csv", "POST", res.post);
//casper.capture("./image.png");
})
casper.run();
回答2:
I finally got the answer after long time RD, we can use download
node module for downloading the attachment as follows
const fs = require('fs');
const download = require('download');
download('http://unicorn.com/foo.pdf').then(data => {
fs.writeFileSync('dist/foo.pdf', data);
});`
Link to Download NPM Module
来源:https://stackoverflow.com/questions/33903418/downloading-a-file-with-casperjs-from-post-attachment