Ultimately, as far as I can tell, you MUST call casper.download( ... ) to fetch the data. (You can dig down into the sources to see what it's doing. Having looked, I decided to stick with the published interface). So if the button has javascript associated with it, use a web proxy (like Charles or Firebug) to see what gets sent to the server. Then write just a bit of code to assemble the same form as would be POSTed when you click the button, and call download() with it.
So for example, the web proxy told me that clicking on this HTML:
<form id="frminvoice" class="pdfer" target="" method="post" action="/Document" novalidate="novalidate">
<input id="id" type="hidden" value="0028816967-0001" name="id">
<input id="type" type="hidden" value="Invoice" name="type">
<input id="acctID" type="hidden" value="245765" name="acctID">
<a id="btnSubmit" class="pdf" href="#">View Invoice</a>
... would POST the following form:
So I gathered together all of the ids, types and acctIDs using simple casper constructs:
var bill_ids = casper.getElementsAttribute('div#myAjaxDiv table tr td form.pdfer input[name="id"]', 'value');
var bill_types = casper.getElementsAttribute('div#myAjaxDiv table tr td form.pdfer input[name="type"]', 'value');
var acct_ids = casper.getElementsAttribute('div#myAjaxDiv table tr td form.pdfer input[name="acctID"]', 'value');
and assembled them into a list of "bill_reference" objects, which not coincidentally happen to be the exact form required for a POST message:
var bill_references = bill_ids.map(function(_, i) {
return { id: bill_ids[i], type: bill_types[i], acctID: acct_ids[i] };
});
Then I just iterated over the bill_references to download each PDF:
for (var index = 0; index < n; index++) {
downloader.downloadOneBill(bill_references[index], index);
}
MyDownloader.prototype.downloadOneBill = function(bill_reference, index) {
var downloader = this;
var casper = this.casper();
casper.then(function b01() {
var url = downloader.BILL_POST_URL;
var form = downloader.createBillDownloadForm(bill_reference);
var target = downloader.createBillFilename(bill_reference);
target, 'POST', form);
});
}