是否可以在 CasperJS 中创建 "for" 循环?

Is it possible to make "for" loop in CasperJS?

的附加问题

我试过这个代码

function execOnce(casper, i, max){
    // end condition
    if (i === max) {
        return;
    }

    casper.wait(3000, function() {
        var button = x('//*[@id="content"]/div[3]/a['+i+']');

        if (!this.exists(button)) {
            this.echo(i + " not available");
            return;
        }

        this.thenClick(button, function (){
            console.log('Searching dic');
            words = words.concat(this.evaluate(getWords));

            // recursive step
            execOnce(this, i+1, max);
        });
    });
};

// start the recursive chain
casper.then(function(){
    execOnce(this, 1, 200);
});

但是我发现目标网页的索引的Xpath有迭代。

当到达'//*[@id="mArticle"]/div[2]/a['11']'时,下一个索引的Xpath变为'//*[@id="mArticle"]/div[2]/a['2'](回到['2'])

例如网页 url 是“http://krdic.naver.com/search.nhn?query=%E3%85%8F%E3%85%8F&kind=keyword

页面下有[1][2][3][4][5][6][7][8][9][10] [Next Page]

当我单击下一页时,您可以看到

 [Previous Page][11][12][13][14][15][16][17][18][19][20] [Next Page]

但是 [12] 的 Xpath 不是 //*[@id="content"]/div[3]/a[12] ---> 它是

//*[@id="content"]/div[3]/a[2]

所以我必须对 function execOnce 进行迭代,包括代码 casper.wait(6000, function() {}

因为我的目标网站对查询非常敏感,所以我尽可能地输入 "wait" 代码..!

在这种情况下,我可以像这样使用嵌套函数吗?

function execOnce(casper, i, max){
    if (i === max) {
        function execOnce(casper, i, max){
            return;
        }
        ...

XPath 非常有表现力。例如,您可以 select 基于 link 文本而不是 link 位置 (//div[@class='paginate']/a[text()='5']) 的预期页面 link,但仅此一项对您没有多大帮助在这种情况下。

问题当然是网站有二级分页。您需要进入下一个分页,才能点击下一个分页links.

casper.wait(3000, function() {
    var nextButton = x('//*[@id="content"]/div[3]/a[text()="'+i+'"]');
    var lastPageNextButton = '.paginate > strong + a.next';
    var button = nextButton;

    if (this.exists(lastPageNextButton)) {
        button = lastPageNextButton;
    } else if (!this.exists(button)) {
        this.echo(i + " not available");
        return;
    }

    this.thenClick(button, function (){
        console.log('Searching dic');
        words = words.concat(this.evaluate(getWords));

        // recursive step
        execOnce(this, i+1, max);
    });
});