How can I wait for the page to be ready in PhantomJS?

后端 未结 2 994
南方客
南方客 2020-12-28 10:50

I\'m using PhantomJS to log into a site an do something. The site used OAuth for logging in. Clicking on the \"Login\" button on the, takes you to the OAuth service. There y

相关标签:
2条回答
  • 2020-12-28 11:16

    An alternate method would be to extend the phantomjs waitfor.js example.

    I use this personnal blend of method. This is my main.js file:

    'use strict';
    
    var wasSuccessful = phantom.injectJs('./lib/waitFor.js');
    var page = require('webpage').create();
    
    page.open('http://foo.com', function(status) {
      if (status === 'success') {
        page.includeJs('https://cdnjs.cloudflare.com/ajax/libs/jquery/3.1.1/jquery.min.js', function() {
          waitFor(function() {
            return page.evaluate(function() {
              if ('complete' === document.readyState) {
                return true;
              }
    
              return false;
            });
          }, function() {
            var fooText = page.evaluate(function() {
              return $('#foo').text();
            });
    
            phantom.exit();
          });
        });
      } else {
        console.log('error');
        phantom.exit(1);
      }
    });
    

    And the lib/waitFor.js file (which is just a copy and paste of the waifFor() function from the phantomjs waitfor.js example):

    function waitFor(testFx, onReady, timeOutMillis) {
        var maxtimeOutMillis = timeOutMillis ? timeOutMillis : 3000, //< Default Max Timout is 3s
            start = new Date().getTime(),
            condition = false,
            interval = setInterval(function() {
                if ( (new Date().getTime() - start < maxtimeOutMillis) && !condition ) {
                    // If not time-out yet and condition not yet fulfilled
                    condition = (typeof(testFx) === "string" ? eval(testFx) : testFx()); //< defensive code
                } else {
                    if(!condition) {
                        // If condition still not fulfilled (timeout but condition is 'false')
                        console.log("'waitFor()' timeout");
                        phantom.exit(1);
                    } else {
                        // Condition fulfilled (timeout and/or condition is 'true')
                        // console.log("'waitFor()' finished in " + (new Date().getTime() - start) + "ms.");
                        typeof(onReady) === "string" ? eval(onReady) : onReady(); //< Do what it's supposed to do once the condi>
                        clearInterval(interval); //< Stop this interval
                    }
                }
            }, 250); //< repeat check every 250ms
    }
    

    This method is not asynchronous but at least am I assured that all the resources were loaded before I try using them.

    0 讨论(0)
  • 2020-12-28 11:20

    It seems that the only way to do this was to use callbacks from the DOM to PhantomJS.

    var page = require('webpage').create();
    var system = require('system');
    
    page.onInitialized = function() {
        page.onCallback = function(data) {
            console.log('Main page is loaded and ready');
            //Do whatever here
        };
    
        page.evaluate(function() {
            document.addEventListener('DOMContentLoaded', function() {
                window.callPhantom();
            }, false);
            console.log("Added listener to wait for page ready");
        });
    
    };
    
    page.open('https://www.google.com', function(status) {});
    
    0 讨论(0)
提交回复
热议问题