如何使PhantomJS等待页面上的特定条件 [英] How to make PhantomJS wait for a specific condition on the page
问题描述
我想实现这样的东西:
function(){
var ua = page.evaluate(function() {
while (document.getElementById("td_details87").parentElement.children[11].textContent == "Running" ) {
console.log ("running.....");
sleep(10000); //10 seconds
};
console.log ("DONE");
});
},
我如何实现睡眠功能,是否有一个while循环?
How can I realize the sleep function and is there a while loop?
推荐答案
JavaScript中没有阻塞sleep()
函数.如果您想睡觉,则必须使用某些异步功能,例如setTimeout(callback, timeout)
.
There is no blocking sleep()
function in JavaScript. If you want to sleep then you have to use some asynchronous function like setTimeout(callback, timeout)
.
PhantomJS的examples文件夹中有一个函数可以执行您要尝试的操作.它是 waitFor(testFx, onReady[, timeout])
.通过一遍又一遍地调用测试函数直到返回真实值来进行工作.
There is a function in the examples folder of PhantomJS that does what you're trying to do. It's waitFor(testFx, onReady[, timeout])
. It works by calling the test function over and over again until it returns a truthy value.
在您的情况下,看起来像这样:
In your case that would look like this:
waitFor(function _test(){
return page.evaluate(function() {
return document.getElementById("td_details87").parentElement.children[11].textContent == "Running";
});
}, function _onReady(){
console.log ("DONE");
});
这篇关于如何使PhantomJS等待页面上的特定条件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!