Skip to content

Instantly share code, notes, and snippets.

@n1k0
Last active August 29, 2016 17:26
Show Gist options
  • Save n1k0/4674914 to your computer and use it in GitHub Desktop.
Save n1k0/4674914 to your computer and use it in GitHub Desktop.
A possible clickWhileSelector() implementation

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.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment