phantomjs not waiting for “full” page load

前端 未结 14 1249
南旧
南旧 2020-11-22 13:48

I\'m using PhantomJS v1.4.1 to load some web pages. I don\'t have access to their server-side, I just getting links pointing to them. I\'m using obsolete version of Phantom

14条回答
  •  说谎
    说谎 (楼主)
    2020-11-22 14:38

    This is an implementation of Supr's answer. Also it uses setTimeout instead of setInterval as Mateusz Charytoniuk suggested.

    Phantomjs will exit in 1000ms when there isn't any request or response.

    // load the module
    var webpage = require('webpage');
    // get timestamp
    function getTimestamp(){
        // or use Date.now()
        return new Date().getTime();
    }
    
    var lastTimestamp = getTimestamp();
    
    var page = webpage.create();
    page.onResourceRequested = function(request) {
        // update the timestamp when there is a request
        lastTimestamp = getTimestamp();
    };
    page.onResourceReceived = function(response) {
        // update the timestamp when there is a response
        lastTimestamp = getTimestamp();
    };
    
    page.open(html, function(status) {
        if (status !== 'success') {
            // exit if it fails to load the page
            phantom.exit(1);
        }
        else{
            // do something here
        }
    });
    
    function checkReadyState() {
        setTimeout(function () {
            var curentTimestamp = getTimestamp();
            if(curentTimestamp-lastTimestamp>1000){
                // exit if there isn't request or response in 1000ms
                phantom.exit();
            }
            else{
                checkReadyState();
            }
        }, 100);
    }
    
    checkReadyState();
    

提交回复
热议问题