Phantomjs is a WebKit -based server-side JavaScript API. It fully supports the web without the need for browser support, its fast, native support for various web standards: DOM processing, CSS selectors, JSON, Canvas, and SVG. Phantomjs can be used for page automation, network monitoring, web screen screenshots, and no interface testing.
First, installation
Installation package: http://phantomjs.org/download.html, including Windows, Mac os,linux version, choose the corresponding version of the download decompression ( for ease of use, you can set the environment variable for PHANTOMJS ), with a example folder with a lot of code already written for use. This article assumes that the PHANTOMJS is already installed and environment variables have been set.
Second, use Hello, world!
Create a new text file that contains the following two lines of script:
Console. log (' Hello, world! '); Phantom. exit ();
Save the file as hello.js , and then execute it:
Phantomjs Hello.js
The output is: Hello, world!
The first line prints out the string at the terminal, and the second row phantom.exit exits the run.
It is important to call in this script phantom.exit , otherwise PHANTOMJS will not stop at all.
Script Parameters –script Arguments
How do phantomjs pass parameters? As shown below:
Phantomjs examples/arguments.js Foo Bar baz
where foo, bar, Baz is the parameter to pass, how to get it:
Require (' system '); if (System.args. 1) {Console. Log (System.args.forEach (ARG, i) {console.': ' + arg);}); Phantom. exit ();
It will output:
FoobarBaz
Page Loading –page Loading
By creating a Web Page object, a Web page can be loaded, parsed, and rendered.
The following script will be the simplest use of the sample Page object, which loads the example.com and saves it as a picture example.png .
Require (' webpage '). Create ();p Age.open (() {Page.render (' Example.png '); Phantom. Exit ();});
Because of this feature, PHANTOMJS can be used to screen screenshots, to intercept some of the content of the snapshot, such as the page, SVG saved into a picture, PDF, etc., this function is very ox x.
The next loadspeed.js script loads a special URL (do not forget the HTTP protocol) and metered the time it takes to load the page.
var page =Require' webpage '). Create (),system =require ( if (system.args. Length = = 1) {console. Log ( ' Usage:loadspeed.js <some url> '); Phantom.system.args[1];p age. Open (Address, function (status) {if (status!== ' success ') {Console.log (else {t = Date.now ()-T; console. Log ( ' Loading time ' + t + ' msec ');} phantom. Exit ();});
Run the script at the command line:
Phantomjs loadspeed.js http://www.google.com
It outputs something like the following:
Loading http://www.google.com Loading Time 719 msec
Code Operation –code Evaluation
To operate on JavaScript or coffeescript in the context of a Web page, use the evaluate() method. The code runs in the sandbox, and it has no way to read any JavaScript objects and variables that are outside the context of the page to which it belongs. evaluate()returns an object, however it is limited to simple objects and cannot contain methods or closures.
Here's an example to display the page title:
Require (var title = Page.evaluate (return document.title;}); Console.log (' page title is ' + (title);});
Any console information that comes from a Web page and includes evaluate() internal code is not displayed by default. To override this behavior, using onConsoleMessage a callback function, the previous example can be rewritten as:
Require (msg) {Console.log (status) {Page.evaluate (() {console.log (document.title);});});
DOM Operation –dom Manipulation
Because the script appears to be running on a Web browser, standard DOM scripts and CSS selectors work well. This makes the PHANTOMJS suitable for supporting a variety of page automation tasks.
The following useragent.js will read the attributes of an element with ID myagent textContent :
var page = require ( ' webpage '). Create (); Console.log ( ' The default User agent is ' + page.settings.userAgent);p age.settings.userAgent = Span class= "string" > ' specialagent ';p age.open ( ' http://www.httpuseragent.org ', function if ( Status!== ' success ') {Console.log (else {var ua = page.evaluate ( function () {return document.getElementById (exit ();
The example above also provides a custom user agent method.
Use jquery and other class libraries:
var page = require (' webpage '). Create ();p Age.open (function () { Page.includejs (function () { Page.evaluate (function () {$ ("button"). Click ();}); Phantom.exit ()} );
Network request and Response –network requests and responses
When a page is requested from a remote server for a resource, both the request and the response can be traced through the onResourceRequested onResourceReceived callback method. Example Netlog.js:
var page = require (request) { Console.log ((response) {Console.log (4));}; Page.open (URL);
For more information on how to use this feature for HAR output and YSlow-based performance analysis, see the Network Monitoring page.
PHANTOMJS Official website: http://phantomjs.org/
Github:https://github.com/ariya/phantomjs/wiki/quick-start
Like a little bit.
Phantomjs Quick Start Tutorial