如何在条件满足时打破CasperJS的重复功能? [英] How to break CasperJS' repeat function when a condition is fulfilled?

查看:70
本文介绍了如何在条件满足时打破CasperJS的重复功能?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道我是否可以打破一个casper.repeat循环。

I want to know if i can break a casper.repeat loop.

我有这个脚本执行此操作..:

I have this script which does this..:

搜索谷歌代理商001,代理商002,代理商003,代理商004,代理商005,代理商006 ..... ....'代理商011。

Searches google for agent 001, agent 002, agent 003, agent 004, agent 005, agent 006..... ....'til agent 011.

我希望它在找到文本James Bond之后停止循环。

I want it to stop looping after it finds the text "James Bond".

现在它找到它,打印出来,但我不知道if和如何停止casper.repeat循环。

Now it finds it, prints it out, but i dont know if and how to stop the casper.repeat loop.

var casper = require("casper").create({
  clientScript: ["jquery.min.js"],
  verbose: true,
  logLevel: "info"
});
var mouse = require("mouse").create(casper);
var x = require('casper').selectXPath;
var webPage = require('webpage');
var page = webPage.create();

casper.on("remote.message", function(msg){
    this.echo("remote> " + msg);
    var test = msg;
    if( test.indexOf('James Bond') >= 0){
    casper.echo("Am Gasit James Bond");
}
});

casper.userAgent('Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/39.0.2171.71 Safari/537.36')
casper.start("https://www.google.com/ncr", function() {
      this.echo("\nINCEPUTUL INCEPUTULUI TITLUL PAGINII IN START (LINIA DE MAI JOS)\n"+this.getTitle()+"\n");
      }).viewport(1366,768);
casper.options.waitTimeout = 30000;
var variabila_mea = "agent ";
var numTimes = 11, count = 1;
casper.repeat(numTimes, function() {
if (count < 10) {
var i = "00"+count;
    casper.waitForSelector(x('//*[@id="gbqfq"]'), function(){
        this.evaluate(function(count, variabila_mea, i, numar) {
            document.getElementsByClassName('gbqfif')[0].value=variabila_mea+i;
            document.forms[0].submit();
            nextPage(count);
        }, ++count,variabila_mea , i,"00000");
        console.log(variabila_mea);

        casper.waitForSelector(x('//*[@id="gbqfq"]'), function(){
            var inputValue = casper.evaluate(function () {
                console.log("\n\n\n"+document.getElementsByClassName('rc')[0].outerHTML+"\n\n\n");
            });
        });

        casper.wait(1000, function(){
        console.log("\n_____________________");
        casper.capture('aa'+i+'.png');
        console.log("_____________________\n");
        });
    });

    } else if (count < 100 && count > 9) {
    var i = "0"+count;
    casper.waitForSelector(x('//*[@id="gbqfq"]'), function(){
        this.evaluate(function(count, variabila_mea, i, numar) {
            document.getElementsByClassName('gbqfif')[0].value=variabila_mea+i;
            document.forms[0].submit();
            nextPage(count);
        }, ++count,variabila_mea , i,"00000");
        console.log(variabila_mea);

        casper.waitForSelector(x('//*[@id="gbqfq"]'), function(){
            var inputValue = casper.evaluate(function () {
                console.log("\n\n\n"+document.getElementsByClassName('rc')[0].outerHTML+"\n\n\n");
            });
        });

        casper.wait(1000, function(){
        console.log("\n_____________________");
        casper.capture('aa'+i+'.png');
        console.log("_____________________\n");
        });
    });

    } else {
    var i = count;
    casper.waitForSelector(x('//*[@id="gbqfq"]'), function(){
        this.evaluate(function(count, variabila_mea, i, numar) {
            document.getElementsByClassName('gbqfif')[0].value=variabila_mea+i;
            document.forms[0].submit();
            nextPage(count);
        }, ++count,variabila_mea , i,"00000");
        console.log(variabila_mea);

        casper.waitForSelector(x('//*[@id="gbqfq"]'), function(){
            var inputValue = casper.evaluate(function () {
                console.log("\n\n\n"+document.getElementsByClassName('rc')[0].outerHTML+"\n\n\n");
            });
        });

        casper.wait(1000, function(){
        console.log("\n_____________________");
        casper.capture('aa'+i+'.png');
        console.log("\n_____________________");
        });
    });

    }

});
casper.run();


推荐答案

您应该了解 casper.repeat();
重复的内部结构如下。取自git hub

You should understand the working of casper.repeat(); Internal structure of repeat is below. Taken from git hub

/**
 * Repeats a step a given number of times.
 * @param  Number    times  Number of times to repeat step
 * @aram   function  then   The step closure
 * @return Casper
 * @see    Casper#then
 */
Casper.prototype.repeat = function repeat(times, then) {
    "use strict";
    for (var i = 0; i < times; i++) {
        this.then(then);
    }
    return this;
};

从这里你可以看到一旦重复被调用,即重复(numTimes,你的函数)。

From this you can see that once repeat is called ie repeat (numTimes,your function).

您可以将then参数视为您要传递的函数重复。所以一旦调用发生,它将在for循环中工作。所以在通话结束后打破重复是不可能的。在你的函数中使用 Casper.byPassIf Casper.byPassUnless ,然后传递给repeat以绕过执行。
使用此功能,您可以跳过功能中的步骤。这意味着重复只有在达到最终计数后才会停止。但它不会执行任何步骤,因为您绕过具有上述功能的步骤。旁路功能的进一步说明不在此范围内。实际上它是你可以尝试的工作。希望它会有所帮助。如果需要进一步澄清,请随时询问。享受!!!

You can consider "then" parameter as what ever the function you are passing to repeat. So once the call is happened it will work inside a for loop. So Breaking the Repeat after the call is not possible. Use Casper.byPassIf Casper.byPassUnless inside your function which you are passing to the repeat in order to bypass the execution. Using this you an skip the steps inside your function. Which means repeat will stop only after reaching it end count. But it won't perform any steps because you are bypassing those with above mentioned functions. Further explanation of bypass function not comes under this scope of question. Actually it is work around you can try. Hope it will be helpful. If needed further clarification please feel free to ask. Enjoy !!!

示例下面的循环将执行10次,但只打印1到5次。

Example the below loop will execute 10 times but only print 1 to 5.

var i=1;
loopcount=10
casper.repeat(loopCount, function() {
    casper.thenBypassIf(function() {
        return i >5;
    }, 1);
    casper.then(function() {
        this.echo (i);
    });
    casper.then(function() {
        i++;
    });     
});

这篇关于如何在条件满足时打破CasperJS的重复功能?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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