Click on all links matching a selector

前端 未结 3 1589
抹茶落季
抹茶落季 2020-12-19 05:22

I have a list of links that I have to simulate a click on using CasperJS. They all share the same class.

However using this.click(\'.click-me\') only cl

3条回答
  •  星月不相逢
    2020-12-19 05:54

    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 =
            '' +
            'link 1' +
            'link 2' +
            'link 3' +
            '';
    });
    
    casper.clickWhileSelector('a').run();
    

    That gives:

    $ casperjs c.js
    found link: link 1
    found link: link 2
    found link: link 3
    Done.
    

提交回复
热议问题