我在 Nightmare js的.wait()函数中遇到麻烦。我试图等待javascript函数返回true,然后继续执行脚本的其余部分。有没有办法做到这一点,因为我尝试过的两种方式都不起作用。谢谢,感谢您的帮助。
var ifCompleted = function(){
for(var i = 0; i < 101; i++){
if(i == 100){
return true;
}
}
}
nightmare
.goto('http://www.google.com')
.wait(function () {
return typeof ifCompleted() !== undefined;
})
.goto('http://www.yahoo.com/')
.catch(function (error) {
console.error('Search failed:', error);
});
下一个等待功能也不起作用。
.wait(function () {
return (ifCompleted() === true);
})
这也不起作用。
.wait(function () {
return (ifCompleted() != null);
})
这段代码是假设的,.wait(100)不是我要尝试执行的操作
最佳答案
我认为您想使用then()。
const Nightmare = require('nightmare');
var nightmare = Nightmare({
show: true,
});
nightmare
.goto('http://www.google.com')
.wait(1000)
.evaluate(function() {
return "Return after 1 second";
})
.then(function(result) {
console.log(result);
nightmare.goto('http://www.yahoo.com/')
.wait(1000)
.then(function() {
console.log("Welcome to yahoo.")
}).catch(function(err) {
console.log(err);
});
}).catch(function(err) {
console.log(err);
});
您可能要检查Check https://github.com/rosshinkley/nightmare-examples/blob/master/docs/common-pitfalls/async-operations-loops.md
关于javascript - 等待 Nightmare js中的javascript函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44489671/