2

I almost have this working, I just can't seem to download the file when it comes up. What am I doing wrong here? When clicking the button "Download Sales Report" a CSV should download, by my console.log() never even fires off.

var casper = require('casper').create();
casper.start('http://www.waynecountyauditor.org/Reports.aspx?ActiveTab=Sales')
.waitForText("Accept")
.thenClick('#ctl00_ContentPlaceHolder1_btnDisclaimerAccept')
.waitForText("View Sales")
.thenClick('#ctl00_ContentPlaceHolder1_WeeklySales_fvSalesReport_btnViewSales')
.waitForText("Download Sales Report")
.thenClick(x('//*[@id="ctl00_blSearchLinks"]/li[4]/a'))
.wait(1000)
.on('page.resource.received', function(resource) {
console.log('here');
if (resource.stage !== "end") {
    return;
}
if (resource.url.indexOf('results.csv') > -1) {
    this.download(resource.url, 'D:\Jobs\Currency\testing\ExportData.csv');
}

});
casper.run();
Adam
  • 153
  • 1
  • 1
  • 7
  • Have you tried registering to "page.resource.received" before the last `thenClick` or inside of the `thenClick` callback? – Artjom B. Nov 24 '15 at 21:59
  • I assume you meant this? And no, this doesn't work either. `.thenClick(x('//*[@id="ctl00_blSearchLinks"]/li[4]/a'),function(){ casper.on('page.resource.received', function(resource) { console.log('here'); if (resource.stage !== "end") { return; } if (resource.url.indexOf('results.csv') > -1) { this.download(resource.url, 'D:\Jobs\Currency\testing\ExportData.csv'); } }); })` – Adam Nov 24 '15 at 22:34
  • @Adam I ran your script and noticed that `resource.url` does not contain string "results.csv", but rather address of the page from which you download. So it's better to do: ` if (resource.contentType.indexOf('text/csv') > -1) { this.download(resource.url, './ExportData.csv'); } ` But it won't work either, because to receieve the file you must POST form and casper.download GETs it by default. I tried to adapt [this answer](http://bit.ly/1PYhbwh) for downloading with POST but for some reason it just won't work: request only reloads the page. Work in progress: http://pastebin.com/974wF4WR – Vaviloff Nov 25 '15 at 07:49
  • If only we could get raw response body from CasperJS/PhantomJS. – Vaviloff Nov 25 '15 at 07:50
  • Thanks for looking into it Vaviloff. I've tried to adapt pretty much every answer I could find and I've been beating my head against the wall. Hopefully we can get something. – Adam Nov 25 '15 at 14:26

2 Answers2

1

I finally figured it out. Vaviloff had 99% of what I needed. I was just missing 2 post variables. Thanks for your help 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();
Adam
  • 153
  • 1
  • 1
  • 7
-1

I finally got the answer after long time RD, we can use download node module for downloading the attachment as follows

const fs = require('fs');

const download = require('download');

download('http://unicorn.com/foo.pdf').then(data => {
    fs.writeFileSync('dist/foo.pdf', data);
});`

Link to Download NPM Module

Kukic Vladimir
  • 1,010
  • 4
  • 15
  • 22