GeezerVisonThe World Wide Web, restored to 800 pixels of glory. |
| Home|About|Gaming cabinet|Archives|How it works|Starter sites |
GeezerVison is showing phantomjs.org — Quick Start with PhantomJS Quick Start with PhantomJSPhantomJS is a command-line tool. Make sure that you are familiar with the use of the command prompt or PowerShell (on Windows) or a terminal (on macOS and Linux). This instruction assumes that PhantomJS is installed and placed somewhere in the PATH (e.g. see this tutorial for Windows users). Hello, World!Create a new text file that contains the following two lines: console.log('Hello, world!');
phantom.exit();
Save it as hello.js and then run it from the terminal or command prompt:
phantomjs hello.jsThe output is: Hello, world!In the first line, console.log will print the passed string to the terminal. In the second line, phantom.exit terminates the execution.
It is very important to call phantom.exit at some point in the script, otherwise PhantomJS will not be terminated at all.
Page LoadingA web page can be loaded, analyzed, and rendered by creating a The following script demonstrates the simplest use of page object. It loads var page = require('webpage').create();
page.open('http://example.com', function(status) {
console.log("Status: " + status);
if(status === "success") {
page.render('example.png');
}
phantom.exit();
});
Because of its rendering features, PhantomJS can be used to capture web pages, essentially taking a screenshot of the contents. The following var page = require('webpage').create(),
system = require('system'),
t, address;
if (system.args.length === 1) {
console.log('Usage: loadspeed.js [some URL]');
phantom.exit();
}
t = Date.now();
address = system.args[1];
page.open(address, function(status) {
if (status !== 'success') {
console.log('FAIL to load the address');
} else {
t = Date.now() - t;
console.log('Loading ' + system.args[1]);
console.log('Loading time ' + t + ' msec');
}
phantom.exit();
});
Run the script with the command: phantomjs loadspeed.js http://www.google.com It outputs something like: Loading http://www.google.com Loading time 719 msec Code EvaluationTo evaluate JavaScript code in the context of the web page, use Here is an example to show the title of a web page: var page = require('webpage').create();
page.open(url, function(status) {
var title = page.evaluate(function() {
return document.title;
});
console.log('Page title is ' + title);
phantom.exit();
});
Any console message from a web page, including from the code inside var page = require('webpage').create();
page.onConsoleMessage = function(msg) {
console.log('Page title is ' + msg);
};
page.open(url, function(status) {
page.evaluate(function() {
console.log(document.title);
});
phantom.exit();
});
Since the script is executed as if it is running on a web browser, standard DOM scripting and CSS selectors work just fine. It makes PhantomJS suitable to carry out various page automation tasks. Further StudyThe code shown above is also available in various examples included with PhantomJS. You are also recommended to explore the use of PhantomJS for page automation, network monitoring, screen capture, and headless testing. |
| You are visitor 01402792 — GeezerVison does not store the pages you look up. Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required. |