【问题标题】:Download CSV after clicking link using CasperJS使用 CasperJS 单击链接后下载 CSV
【发布时间】:2015-09-21 13:58:31
【问题描述】:

我创建了一个脚本,用于登录我的银行帐户,导航到交易页面,然后尝试下载所有交易数据的 CSV。但是,单击“下载”按钮后,资源永远不会被下载。单击按钮时调用的资源是“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");
});

这是检查员的图片,以防这些细节有助于澄清问题。

更新: 我也试过了,但是“下载”点击事件后调试器没有输出。

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。也许这暗示出了什么问题?

【问题讨论】:

标签: javascript download click casperjs


【解决方案1】:

单独单击链接似乎不会导致应该发生的 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 函数。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多