phantomjs not waiting for “full” page load

前端 未结 14 1178
南旧
南旧 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:39

    Here is a solution that waits for all resource requests to complete. Once complete it will log the page content to the console and generate a screenshot of the rendered page.

    Although this solution can serve as a good starting point, I have observed it fail so it's definitely not a complete solution!

    I didn't have much luck using document.readyState.

    I was influenced by the waitfor.js example found on the phantomjs examples page.

    var system = require('system');
    var webPage = require('webpage');
    
    var page = webPage.create();
    var url = system.args[1];
    
    page.viewportSize = {
      width: 1280,
      height: 720
    };
    
    var requestsArray = [];
    
    page.onResourceRequested = function(requestData, networkRequest) {
      requestsArray.push(requestData.id);
    };
    
    page.onResourceReceived = function(response) {
      var index = requestsArray.indexOf(response.id);
      if (index > -1 && response.stage === 'end') {
        requestsArray.splice(index, 1);
      }
    };
    
    page.open(url, function(status) {
    
      var interval = setInterval(function () {
    
        if (requestsArray.length === 0) {
    
          clearInterval(interval);
          var content = page.content;
          console.log(content);
          page.render('yourLoadedPage.png');
          phantom.exit();
        }
      }, 500);
    });
    

提交回复
热议问题