如何保存由 CasperJS 中的按钮单击触发的文件?

How do I save a file triggered by button click in CasperJS?

我正在尝试下载按日期范围填写的日志,所以我用我想要的日期填写表格,然后当您单击 "Export Call Logs" 按钮时,它会自动触发 CSV 文件下载在常规浏览器中。

如何保存使用 Casper 'clicking' 相同按钮时应自动触发的文件?

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

按钮HTML如下:

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

此外,作为旁注,显然我不想手动输入日期,在某种程度上有点违背了程序的意义,我最熟悉 Pyhon,它们是否等同于DateTime 模块或者我可以使用 Casper 获取前几天的日期并将其存储为 Var 以进行相应输入?即今天的日期是 08/31/2016 我想输入前一天,08/30/2016。

编辑: 尝试实施下面评论的 example

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

您可以试试download()函数,您可以找到更多信息here

有个example.