0

I am trying to download a log that is filled by date range, so I fill out the form with the dates I want and then when you click the "Export Call Logs" button it just automatically triggers a CSV file download in a regular browser.

How do I save that file that should be automatically triggered when 'clicking' the same button using Casper?

casper.then(function(){
    console.log("Filling out form and getting CSV");
    this.evaluate(function(){
        document.getElementsByName("startdate")[0].value="08/30/2016";
        document.getElementsByName("enddate")[0].value="08/30/2016";
        document.getElementsByName("s1")[0].click();
    });
});

The button HTML is as follows:

<td><input type="submit" name="s1" value="Export Call Logs"></td>

Also, as a side note, obviously I don't want to manually input the date, kinda defeats the point of a program in a way, I am most familiar with Pyhon, is their some sort of equivalent to the DateTime module or someway I can use Casper to get the previous days date and store as a Var to input accordingly? i.e todays date is 08/31/2016 I would want to input the previous day, 08/30/2016.

EDIT: Tried implementing the example commented below.

casper.then(function(){
    console.log("Filling out form and getting CSV");
    this.page.onFileDownload = function(status){console.log("onFileDownload(' + status + ')");
    return "downloadedfile.csv"; };
    this.evaluate(function(){
        document.getElementsByName("startdate")[0].value="08/30/2016";
        document.getElementsByName("enddate")[0].value="08/30/2016";
        document.getElementsByName("s1")[0].click();
    });
});
Community
  • 1
  • 1
Mxracer888
  • 341
  • 1
  • 4
  • 14
  • The `page.onFileDownload` callback is only available in the unofficial fork of PhantomJS, but it's the only sane way to do this. – Artjom B. Sep 01 '16 at 18:14

1 Answers1

0

You can try download() function, you can find more information here.

There is an example.

Community
  • 1
  • 1
Sphinx
  • 186
  • 5
  • I saw that, but I'm confused due to the fact there is no URL to use in the download function. I tried the example you posted, but don't know if I have implemented it correctly. Can you check my OP for the edit to see if it looks right? – Mxracer888 Sep 01 '16 at 16:28
  • If you are using "submit" form, there should be one target URL existing. Check the source code of your generated page in the browser, locate the form around the submit button, you will find the "action url" of form. If possible, you can show me your testing page. – Sphinx Sep 02 '16 at 02:34
  • After some digging I was able to find it. now the download function works! Thanks! – Mxracer888 Sep 02 '16 at 21:58