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

Simulating scrolling in PhantomJS


This recipe introduces the scrollPosition property and how we can use it to simulate scrolling in PhantomJS.

Getting ready

To run this recipe, we will need a script that loads a web page tall enough (or wide enough) to scroll, and our script needs to expect to scroll the page.

The script in this recipe is available in the downloadable code repository as recipe09.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.viewportSize = { width: 1280, height: 800 };
webpage.scrollPosition = { top: 0, left: 0 };

webpage.open('https://twitter.com/founddrama', function(status) {
  if (status === 'fail') {
    console.error('webpage did not open successfully');
    phantom.exit(1);
  }

  var i = 0,
      top,
      queryFn = function(...