Click on all links matching a selector

前端 未结 3 1591
抹茶落季
抹茶落季 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:47

    Mixing the other responses, to avoid the infinite loop (this worked for me, as my items were consecutive inside a tag):

    casper.clickWhileSelector = function(selector, i) {
        return this.then(function() {
            i = i || 1;
            selectorNth = selector+':nth-child(' + i + ')';
    
            if (this.exists(selectorNth)) {
                this.echo('found link: '+this.getElementInfo(selectorNth).tag);
                this.click(selectorNth);
                return this.clickWhileSelector(selector, i+1);
            }
            return this.echo('Done.').exit();
        });
    }
    

    Hope it helps!

    Luis.

提交回复
热议问题