如何在CasperJS中异步点击时停止循环 [英] How to stop a loop when clicking asynchronously in CasperJS

查看:193
本文介绍了如何在CasperJS中异步点击时停止循环的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



最大页数是200,但是如果下面的XPath下面我想停止执行不存在(对于第200页之前的页面)。

如何设置 i 变量?

var casper = require('casper')。
var x = require('casper')。selectXPath;

for(var i = 1; i <= 200; i ++){
casper.wait(6000,function(){

casper.thenClick x('// * [@ id =mArticle] / div [2] / a ['+ i +']'),function(){
console.log('Searching dic');
words = words.concat(this.evaluate(getWords));
});

});


解决方案

CasperJS提供 exists() 功能。所以,你可以像这样重写你的代码:


$ b

  for(var i = 1; i< ; = 200; i ++){
(function(i){
casper.wait(6000,function(){
var button = x('// * [@ id =mArticle )/ div [2] / a ['+ i +']');
if(!this.exists(button)){
this.echo(i +not available);
return; //下面的`thenClick()`不会执行

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

我还添加了一个IIFE,以便您拥有正确的 i 在回调里面。有关更多信息,请参阅 JavaScript闭合内部循环 - 简单实用示例



这是有效的,但是如果假定如果链路100不在那里,则链路101和102等也不在那里。你会等很多(6秒100)。在这种情况下,您需要递归执行此操作,因为CasperJS的异步特性:
$ b

  function execOnce(casper,i,max){
//结束条件
if(i === max){
return;

casper.wait(6000,function(){
var button = x('// * [@ id =mArticle] / div [2] / a ['+如果(!this.exists(button)){
this.echo(i +not available);
return;
}
this.thenClick(button,function(){
console.log('Searching dic');
words = words.concat(this.evaluate(getWords));

//递归步骤
execOnce(this,i + 1,max);
});
});
};

casper.start(url);
$ b $ //启动递归链
casper.then(function(){
execOnce(this,1,200);
});

casper.run();

请注意,现在您已经递归地定义了一个适当的结束条件,页面有什么,什么不是。


I'm crawling multiple pages using CasperJS, but I got stuck.

The maximum number of pages are 200 but I want to stop the execution if the XPath below doesn't exist (for a page before the 200th).

How can I set up i variable?

var casper = require('casper').create();
var x = require('casper').selectXPath;

for (var i=1; i <=200; i++) {
    casper.wait(6000, function() {

        casper.thenClick(x('//*[@id="mArticle"]/div[2]/a['+i+']'), function (){
            console.log('Searching dic');
            words = words.concat(this.evaluate(getWords));
        });

    });
}

解决方案

CasperJS provides the exists() function. So, you can rewrite your code like this:

for (var i=1; i <=200; i++) {
    (function(i){
        casper.wait(6000, function() {
            var button = x('//*[@id="mArticle"]/div[2]/a['+i+']');
            if (!this.exists(button)) {
                this.echo(i + " not available");
                return; // the following `thenClick()` is not executed
            }
            this.thenClick(button, function (){
                console.log('Searching dic');
                words = words.concat(this.evaluate(getWords));
            });
        });
    })(i);
}

I've also added an IIFE, so that you have the correct i inside of the callback. For more information, see JavaScript closure inside loops – simple practical example.

This works, but it is not very efficient if one would assume that if link 100 is not there, then link 101 and 102 etc. are also not there. You would wait a lot (6 seconds times 100). In that case you need to do this recursively, because of the asynchronous nature of CasperJS:

function execOnce(casper, i, max){
    // end condition
    if (i === max) {
        return;
    }
    casper.wait(6000, function() {
        var button = x('//*[@id="mArticle"]/div[2]/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);
        });
    });
};

casper.start(url);

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

casper.run();

Note that now that you have it recursively, you can define a proper end condition by explicitly looking on the page what's there and what isn't.

这篇关于如何在CasperJS中异步点击时停止循环的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆