Anticipating the page loading error

Since we will soon be dealing with page loading, we should have some capabilities to identify whether or not the page is properly loaded. We can do this by checking the status of the loading of the page using the webpage object's open callback.

var system = require('system');
var url = system.args[1];
var page = require('webpage').create();
page.open(url, function(status) {
  if(status == 'success') {
    console.log('Page loaded.');
    // do more stuff here on the loaded page
  } else {
    console.log('Ooops! Problem loading page: ' this.url);
    phantom.exit(1);
  }
});

The open method's second parameter is a callback that will be executed after the page loads, with or without an error. The function callback will have a single ...

Get Getting Started with PhantomJS now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.