如何为 casper.repeat 设置变量值

2024-04-16

我想根据来自运行 CasperJS 的页面的变量值重复使用 CasperJS 的步骤。

为了获得这个值,我做了类似的事情:

casper.waitForSelector(".xxxx", function () {
myvalue = this.evaluate(function() {
        value = Math.ceil(document.querySelector('#yyy').getAttribute('data-all')/10)-1;
        return value;
    });                 
});

然后我尝试做类似的事情:

casper.repeat(myvalue, function() {

但它不起作用,因为重复找不到myvalue多变的。知道如何实现这样的目标吗?

EDIT

现在我尝试这个:

var myvalue = "";                 

casper.waitForSelector(".xxxx", function () {
    myvalue = this.evaluate(function() {
        value = Math.ceil(document.querySelector('#connections').getAttribute('data-num-all')/10)-1;
        return value;
    }); 
});

casper.repeat(myvalue, function() {

现在我没有收到任何语法错误,但重复根本没有执行(myvalue = 49)


I think casper.repeat and casper.waitForSelector是异步执行的,所以repeat()在之前执行waitFor().

尝试一下:

var myvalue = "";                 

casper.waitForSelector(".xxxx", function () {
    myvalue = this.evaluate(function() {
        value = Math.ceil(document.querySelector('#connections').getAttribute('data-num-all')/10)-1;
        return value;
    }); 
});

casper.then(function(){
    casper.repeat(myvalue, function() {
        this.echo("Here the code to be executed 'myvalue' times");
    });
});

then() 语句等待前面的 waitForSelector() 执行完毕,然后再执行 Repeat()。

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

如何为 casper.repeat 设置变量值 的相关文章

随机推荐