How to make PhantomJS wait for a specific condition on the page

百般思念 提交于 2019-12-11 07:58:53

问题


I want to realize something like this:

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");
     });
},

How can I realize the sleep function and is there a while loop?


回答1:


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).

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");
});


来源:https://stackoverflow.com/questions/32502661/how-to-make-phantomjs-wait-for-a-specific-condition-on-the-page

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!