Book Image

PhantomJS Cookbook

By : Rob Friesel
Book Image

PhantomJS Cookbook

By: Rob Friesel

Overview of this book

Table of Contents (15 chapters)
PhantomJS Cookbook
Credits
About the Author
About the Reviewers
www.PacktPub.com
Preface
Index

Inspecting page content from a PhantomJS script


This recipe introduces webpage.evaluate, which provides us with a hook into the context and content of the web page we have requested, including ways to inspect and manipulate the DOM. The cornerstone for many of the recipes that lie ahead will be webpage.evaluate.

Getting ready

To run this recipe, we will need a script that loads a web page, and we will need a callback function to webpage.open that expects to work with the content of the HTTP response.

The script in this recipe is available in the downloadable code repository as recipe04.js under chapter03. If we run the provided example script, we must change to the root directory for the book's sample code. Lastly, for this example to work, we will need an Internet connection.

How to do it…

Consider the following script:

var webpage = require('webpage').create();

webpage.open('http://blog.founddrama.net/', function(status) {
  if (status === 'fail') {
    console.error('Failed to open requested...