Javascript 在循环中嵌套casper.js操作
我正在尝试将casper.then()操作嵌套在while循环中。但是,脚本似乎从不执行那些casper.then()函数中的代码 这是我的密码Javascript 在循环中嵌套casper.js操作,javascript,casperjs,Javascript,Casperjs,我正在尝试将casper.then()操作嵌套在while循环中。但是,脚本似乎从不执行那些casper.then()函数中的代码 这是我的密码 casper.then(function() { while (this.exists(x('//a[text()="Page suivante"]'))) { this.then(function() { this.click(x('//a[text()="Page suivante"]'))
casper.then(function() {
while (this.exists(x('//a[text()="Page suivante"]'))) {
this.then(function() {
this.click(x('//a[text()="Page suivante"]'))
});
this.then(function() {
infos = this.evaluate(getInfos);
});
this.then(function() {
infos.map(printFile);
fs.write(myfile, "\n", 'a');
});
}
});
我错过什么了吗
casper。然后
在队列中安排一个步骤,并且不会立即执行。它仅在上一步完成时执行。由于父级casper.then
包含本质上是的代码,而(true)
,因此它永远不会结束
您需要使用递归对其进行一些更改:
function schedule() {
if (this.exists(x('//a[text()="Page suivante"]'))) {
this.then(function() {
this.click(x('//a[text()="Page suivante"]'))
});
this.then(function() {
infos = this.evaluate(getInfos);
});
this.then(function() {
infos.map(printFile);
fs.write(myfile, "\n", 'a');
});
this.then(schedule); // recursive step
}
}
casper.start(url, schedule).run();