0

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?

Optimus
  • 1,354
  • 1
  • 21
  • 40
  • 1
    Does the file come in as a post request? If so: [downloading a file that comes as an attachment in a POST request response in PhantomJs](http://stackoverflow.com/questions/16144252/downloading-a-file-that-comes-as-an-attachment-in-a-post-request-response-in-pha) – Artjom B. Sep 21 '15 at 14:02
  • No. It's in a GET request. – Optimus Sep 21 '15 at 14:49
  • 1
    Have you tried the solutions? – Artjom B. Sep 21 '15 at 15:06
  • I've tried the one referenced in my Update. There's no output after the button click event (in debugging). PhantomJS 2.0 has a bunch of issues that I've been trying to avoid so the first answer isn't a great solution. – Optimus Sep 21 '15 at 15:13

1 Answers1

0

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.

Optimus
  • 1,354
  • 1
  • 21
  • 40