phantomjs not waiting for “full” page load

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

    This is an old question, but since I was looking for full page load but for Spookyjs (that uses casperjs and phantomjs) and didn't find my solution, I made my own script for that, with the same approach as the user deemstone . What this approach does is, for a given quantity of time, if the page did not receive or started any request it will end the execution.

    On casper.js file (if you installed it globally, the path would be something like /usr/local/lib/node_modules/casperjs/modules/casper.js) add the following lines:

    At the top of the file with all the global vars:

    var waitResponseInterval = 500
    var reqResInterval = null
    var reqResFinished = false
    var resetTimeout = function() {}
    

    Then inside function "createPage(casper)" just after "var page = require('webpage').create();" add the following code:

     resetTimeout = function() {
         if(reqResInterval)
             clearTimeout(reqResInterval)
    
         reqResInterval = setTimeout(function(){
             reqResFinished = true
             page.onLoadFinished("success")
         },waitResponseInterval)
     }
     resetTimeout()
    

    Then inside "page.onResourceReceived = function onResourceReceived(resource) {" on the first line add:

     resetTimeout()
    

    Do the same for "page.onResourceRequested = function onResourceRequested(requestData, request) {"

    Finally, on "page.onLoadFinished = function onLoadFinished(status) {" on the first line add:

     if(!reqResFinished)
     {
          return
     }
     reqResFinished = false
    

    And that's it, hope this one helps someone in trouble like I was. This solution is for casperjs but works directly for Spooky.

    Good luck !

提交回复
热议问题