Search code examples
javascriptdownloadclickcasperjs

Download CSV after clicking link using CasperJS


I've created a script that logs into my bank account, navigates to the transaction page and then attempts to download the CSV of all transaction data. However, after clicking the "Download" button, the resource is never downloaded. The resource that is called on the button click is "download.qfx" and a different filename is generated each time. Any help would be appreciated.

// 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");
});

Here's an image from the inspector in case any of these details help clarify the problem. enter image description here

Update: I also tried, but there was no output in the debugger after the "Download" click event.

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');
    }
});

Additionally, if I type console.log(resource.url), I never see download.qfx. Maybe that hints at what is wrong?


Solution

  • Clicking the link alone did not seem to result in the onClick javascript calls that should have happened. So, then I tested:

    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');
        }
    });
    

    So, for some reason, it was necessary to call the onClick functions separately.