Skip to content
This repository has been archived by the owner on May 30, 2023. It is now read-only.

Page Automation

ariya edited this page Sep 23, 2012 · 10 revisions

Because PhantomJS can load and manipulate a web page, it is perfect to carry out various page automations.

DOM Manipulation

Since the script is executed as if it is running on a web browser, standard DOM scripting and CSS selectors work just fine.

The following useragent.js example demonstrates reading the innerText property of the element whose id is myagent:

var page = require('webpage').create();
console.log('The default user agent is ' + page.settings.userAgent);
page.settings.userAgent = 'SpecialAgent';
page.open('http://www.httpuseragent.org', function (status) {
    if (status !== 'success') {
        console.log('Unable to access network');
    } else {
        var ua = page.evaluate(function () {
            return document.getElementById('myagent').innerText;
        });
        console.log(ua);
    }
    phantom.exit();
});

The above example also demonstrates a way to customize the user agent seen by the remote web server.

Clone this wiki locally