As proposed on the CasperJS ML and for the records, here's a possible implementation of clickWhileSelector
:
var casper = require('casper').create();
casper.clickWhileSelector = function(selector) {
return this.then(function() {
if (this.exists(selector)) {
this.echo('found link: ' + this.getElementInfo(selector).tag);
this.click(selector);
return this.clickWhileSelector(selector);
}
return this.echo('Done.').exit();
});
}
casper.start().then(function() {
this.page.content =
'<html><body>' +
'<a href="#" onclick="this.parentNode.removeChild(this);return false;">link 1</a>' +
'<a href="#" onclick="this.parentNode.removeChild(this);return false;">link 2</a>' +
'<a href="#" onclick="this.parentNode.removeChild(this);return false;">link 3</a>' +
'</body></html>';
});
casper.clickWhileSelector('a').run();
That gives:
$ casperjs c.js
found link: <a href="#" onclick="this.parentNode.removeChild(this);return false;">link 1</a>
found link: <a href="#" onclick="this.parentNode.removeChild(this);return false;">link 2</a>
found link: <a href="#" onclick="this.parentNode.removeChild(this);return false;">link 3</a>
Done.