使用 CasperJS 单击 link 后下载 CSV
Download CSV after clicking link using CasperJS
我创建了一个脚本来登录我的银行帐户,导航到交易页面,然后尝试下载所有交易数据的 CSV。但是,在单击 "Download" 按钮后,资源永远不会下载。单击按钮时调用的资源是 "download.qfx",每次都会生成不同的文件名。任何帮助将不胜感激。
// When download page loads, click the appropriate settings and download transactions
casper.then(function(){
this.waitForSelector("#transactionPeriod", function() {
this.evaluate(function() {
document.querySelector('#transactionPeriod').selectedIndex = 0; //it is obvious
return true;
});
this.clickLabel("Spreadsheet (Comma Separated Values)", "label");
});
});
// Click the download button
casper.then(function(){
casper.click(x("//a[contains(text(), 'Download')]"));
});
// Save the download file
casper.then(function(){
casper.download("https://secure.capitalone360.com/myaccount/download.qfx", "export.csv");
});
这是检查员提供的图片,以防这些细节中的任何一个有助于澄清问题。
更新:
我也试过了,但是"Download"点击事件后调试器没有输出。
casper.then(function(){
casper.click(x("//a[contains(text(), 'Download')]"));
});
casper.on('resource.received', function(resource) {
if (resource.stage !== "end") {
console.log("resource.stage !== 'end'");
return;
}
if (resource.url.indexOf('download.qfx') > -1) {
console.log("Downloading csv file");
this.download(resource.url, 'ExportData.csv');
}
});
此外,如果我输入 console.log(resource.url),我永远不会看到 download.qfx。也许这暗示出了什么问题?
单独单击 link 似乎并没有导致本应发生的 onClick javascript 调用。所以,然后我测试了:
casper.then(function(){
//casper.click(x("//a[contains(text(), 'Download')]"));
casper.evaluate(function(){
urchinTracker('/download_transactions/continue');
submitForm('download');
return false;
});
});
casper.on('resource.received', function(resource) {
//console.log(resource.url);
if (resource.stage !== "end") {
return;
}
if (resource.url.indexOf('download.qfx') !== -1) {
this.download(resource.url, 'ExportData.csv');
}
});
因此,由于某些原因,需要单独调用 onClick 函数。
我创建了一个脚本来登录我的银行帐户,导航到交易页面,然后尝试下载所有交易数据的 CSV。但是,在单击 "Download" 按钮后,资源永远不会下载。单击按钮时调用的资源是 "download.qfx",每次都会生成不同的文件名。任何帮助将不胜感激。
// When download page loads, click the appropriate settings and download transactions
casper.then(function(){
this.waitForSelector("#transactionPeriod", function() {
this.evaluate(function() {
document.querySelector('#transactionPeriod').selectedIndex = 0; //it is obvious
return true;
});
this.clickLabel("Spreadsheet (Comma Separated Values)", "label");
});
});
// Click the download button
casper.then(function(){
casper.click(x("//a[contains(text(), 'Download')]"));
});
// Save the download file
casper.then(function(){
casper.download("https://secure.capitalone360.com/myaccount/download.qfx", "export.csv");
});
这是检查员提供的图片,以防这些细节中的任何一个有助于澄清问题。
更新: 我也试过了,但是"Download"点击事件后调试器没有输出。
casper.then(function(){
casper.click(x("//a[contains(text(), 'Download')]"));
});
casper.on('resource.received', function(resource) {
if (resource.stage !== "end") {
console.log("resource.stage !== 'end'");
return;
}
if (resource.url.indexOf('download.qfx') > -1) {
console.log("Downloading csv file");
this.download(resource.url, 'ExportData.csv');
}
});
此外,如果我输入 console.log(resource.url),我永远不会看到 download.qfx。也许这暗示出了什么问题?
单独单击 link 似乎并没有导致本应发生的 onClick javascript 调用。所以,然后我测试了:
casper.then(function(){
//casper.click(x("//a[contains(text(), 'Download')]"));
casper.evaluate(function(){
urchinTracker('/download_transactions/continue');
submitForm('download');
return false;
});
});
casper.on('resource.received', function(resource) {
//console.log(resource.url);
if (resource.stage !== "end") {
return;
}
if (resource.url.indexOf('download.qfx') !== -1) {
this.download(resource.url, 'ExportData.csv');
}
});
因此,由于某些原因,需要单独调用 onClick 函数。