我終於明白了。瓦維洛夫有99%的我需要。我只是缺少2個變量。感謝您的幫助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();
您是否嘗試過註冊到「page.resource。收到「之前的最後'thenClick'或在'''''''''回調? –
我假設你是這個意思?不,這也行不通。 '.thenClick(X( '// * [@ ID = 「ctl00_blSearchLinks」] /鋰[4]/A'),函數(){ \t casper.on( 'page.resource.received',功能(資源){ \t \t的console.log( '這裏'); \t \t如果{ \t \t \t回報(resource.stage == 「結束」!); \t \t} \t \t如果(資源。 url.indexOf('results.csv')> -1){ \t \t \t this.download(resource.url,'D:\ Jobs \ Currency \ testing \ ExportData.csv'); \t \t} \t}); })' – Adam
@Adam我跑過你的腳本,發現'resource.url'不包含字符串「results.csv」,而是你下載頁面的地址。所以最好這樣做:'if(resource.contentType.indexOf('text/csv')> -1){this.download(resource.url,'./ExportData.csv'); }'但是它也行不通,因爲接受文件你必須POST表單,casper.download默認獲取它。我試圖修改[這個答案](http://bit.ly/1PYhbwh)用POST進行下載,但由於某種原因它不起作用:請求只重新加載頁面。正在進行中:http://pastebin.com/974wF4WR – Vaviloff