如何使casperjs重复循环直到满足特定条件? [英] How can I make casperjs repeat a loop until a certain condition is met?

查看:120
本文介绍了如何使casperjs重复循环直到满足特定条件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试让casperjs在以下情况下工作:

I'm trying to get casperjs working with the following situation:

网页加载,然后在该页面内用ajax加载数据项 以及一个阅读更多"按钮,该按钮又会加载更多数据 项目.

A web page loads, then within that page, it ajax loads data items along with a 'read more' button which in turn loads some more data items.

我需要脚本来递归检查更多"按钮是否存在(因为要加载的数据项很多),如果存在,请单击该按钮,否则继续执行脚本的其余部分,并将整个页面输出为jpeg.

I need the script to recursively check if the 'read more' button exists (as there are many data items to load), if so, click it, else continue with the rest of the script and output the full page as a jpeg.

我已经尝试通过下面的代码编写代码,但是它没有像我希望的那样循环.它只单击一次按钮,然后输出图像,即使加载了更多数据,该按钮仍然存在,可以再次单击.

I've tried by writing the code below, but it doesn't loop as I had hoped. It just clicks the button once, then outputs the image, even though more data loads, and the button still exists for clicking again.

var casper = require('casper').create({
    verbose : true,
    logLevel : 'debug',
    pageSettings : {
        "userAgent" : 'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.10 (KHTML, like Gecko) Chrome/23.0.1262.0 Safari/537.10',
        "loadImages" : false,
        "webSecurityEnabled" : false,
        "ignoreSslErrors" : true
    },
});

var urls = {
    login : 'http://www.website.com/login',
    details : 'http://www.website.com/details',
};

casper.start(urls.login, function() {

    //login stuff

});

//the function I'm trying to recursively loop before moving on to saving the capture image
function checkMore() {

    //check to see if the 'read more' button exists
    if (casper.exists('a.read-more')) {

        //click the button to load more items           
        casper.click('a.read-more');

        //wait for the items to load, then run the check again
        casper.wait(3000, function() {
            casper.run(checkMore);
        });

    }

}

casper.thenOpen(urls.details, function() {

    //wait for the page along with ajax items to load shortly after
    this.wait(3000, function() {
        this.run(checkMore);
    });

});

casper.then(function() {

    //output the result
    this.capture('output.jpg');

});

casper.run();

推荐答案

Pius的想法正确,我修改了代码,解决方法如下.

Pius had the right idea, I modified my code, the solution is below.

var urls = {
    login : 'http://www.website.com/login',
    details : 'http://www.website.com/details',
};

casper.start(urls.login, function() {

    //login stuff

});

//the function I'm trying to recursively loop before moving on to saving the capture image
function checkMore() {

    //check to see if the 'read more' button exists
    if (casper.exists('a.read-more')) {

        //click the button to load more items           
        casper.click('a.read-more');

        //wait for the items to load, then run the check again
        casper.wait(3000, checkMore);

    }

}

casper.thenOpen(urls.details, function() {

    //wait for the page along with ajax items to load shortly after
    this.wait(3000, checkMore);

});

casper.then(function() {

    //output the result
    this.capture('output.jpg');

});

casper.run();

这篇关于如何使casperjs重复循环直到满足特定条件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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