


PhantomJS is a server-side JavaScript API based on WebKit. It fully supports the web without requiring browser support, and it is fast and natively supports various web standards: DOM processing, CSS selectors, JSON, Canvas, and SVG. PhantomJS can be used for page automation, network monitoring, web page screenshots, and interfaceless testing, etc.
PhantomJs official website: http://phantomjs.org/
GitHub:https://github.com/ariya/phantomjs/wiki/Quick-Start
1. Installation
Installation package download address: http://phantomjs.org/download.html, including Windows, Mac OS, and Linux versions. You can choose the corresponding version to download and decompress (for convenience, you can Set environment variables for phantomjs), which has an example folder with a lot of already written code for use. This article assumes that phantomjs has been installed and environment variables have been set.
2. Use
Hello, World!
Create a new text file containing the following two lines of script:
console.log('Hello, world!'); phantom.exit();
Save the file as hello.js and execute it:
phantomjs hello.js
The output result is: Hello, world!
The first line will print out the string in the terminal, and the second line phantom.exit will exit the operation.
It is very important to call phantom.exit in this script, otherwise PhantomJS will not stop at all.
Script Arguments – Script Arguments
How to pass parameters in Phantomjs? As shown below:
phantomjs examples/arguments.js foo bar baz
Foo, bar, baz are the parameters to be passed. How to get them:
var system = require('system'); if (system.args.length === 1) { console.log('Try to pass some args when invoking this script!'); } else { system.args.forEach(function (arg, i) { console.log(i + ': ' + arg); }); } phantom.exit();
It will output:
0: foo
1: bar
2: baz
Page Loading – Page Loading
By creating a web page object, a web page can be loaded, analyzed and rendered.
The following script uses the example page object in its simplest form. It loads example.com and saves it as an image, example.png .
var page = require('webpage').create(); page.open('http://example.com', function () { page.render('example.png'); phantom.exit(); });
Due to this feature of it, PhantomJS can be used to take screenshots of web pages and take snapshots of some content, such as saving web pages and SVGs as images, PDFs, etc. This function is awesome.
The next loadspeed.js script loads a special URL (don't forget the http protocol) and measures the time it takes to load the page.
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 time ' + t + ' msec'); } phantom.exit(); });
Run this script from the command line:
phantomjs loadspeed.js http://www.google.com
It outputs something like:
Loading http://www.google.com Loading time 719 msec
Code Evaluation – Code Evaluation
To evaluate JavaScript or CoffeeScript in the context of a web page, use the evaluate() method. The code runs in a "sandbox" and has no way of reading any JavaScript objects and variables outside the context of the page it belongs to. evaluate() returns an object, however it is limited to simple objects and cannot contain methods or closures.
Here is an example to display the page title:
var page = require('webpage').create(); page.open(url, function (status) { var title = page.evaluate(function () { return document.title; }); console.log('Page title is ' + title); });
Any console information from the web page and including code from evaluate() will not be displayed by default. To override this behavior, use the onConsoleMessage callback function. The previous example can be rewritten as:
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); }); });
DOM Manipulation – DOM Manipulation
Since the script runs as if it were a web browser, standard DOM scripts and CSS selectors work fine. This makes PhantomJS suitable for supporting a variety of page automation tasks.
The following useragent.js will read the textContent attribute of the element with the id 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').textContent; }); console.log(ua); } phantom.exit(); });
The above example also provides a way to customize user agent.
Use JQuery and other libraries:
var page = require('webpage').create(); page.open('http://www.sample.com', function() { page.includeJs("http://ajax.googleapis.com/ajax/libs/jquery/1.6.1/jquery.min.js", function() { page.evaluate(function() { $("button").click(); }); phantom.exit() }); });
Network requests and responses – Network Requests and Responses
When a page requests a resource from a remote server, both the request and the response can be tracked through the onResourceRequested and onResourceReceived callback methods. Example netlog.js:
var page = require('webpage').create(); page.onResourceRequested = function (request) { console.log('Request ' + JSON.stringify(request, undefined, 4)); }; page.onResourceReceived = function (response) { console.log('Receive ' + JSON.stringify(response, undefined, 4)); }; page.open(url);
For more information on how to use this feature for HAR output and YSlow-based performance analysis, please refer to the Network Monitoring Page .

我们通过学习如何使用mojs为HTML元素添加动画来开始本系列。在第二个教程中,我们继续使用Shape模块制作内置SVG形状的动画。第三个教程介绍了使用ShapeSwirl和stagger模块对SVG形状进行动画处理的更多方法。现在,我们将学习如何使用Burst模块以突发形式制作不同SVG形状的动画。本教程将取决于我们在前三个教程中介绍的概念。如果您还没有阅读过它们,我建议您先阅读它们。创建基本连拍动画在创建任何突发动画之前,我们需要做的第一件事是实例化Burst对象。之后,我们可以指定不同属性

快速入门:Python安装pandas库的方法,需要具体代码示例一、概述Python是一种广泛使用的编程语言,它拥有强大的开发生态系统,其中包括许多实用的库。而pandas是其中一款非常受欢迎的数据分析库,它提供了高效的数据结构和数据分析工具,使得数据处理和分析变得更加简单。本文将介绍如何在Python中安装pandas库,并提供相应的代码示例。二、安装Py

快速入门:使用Go语言函数实现简单的音频流媒体服务引言:音频流媒体服务在今天的数字化世界中越来越受欢迎,它可以让我们通过网络直接播放音频文件,而无需进行完整的下载。本文将介绍如何使用Go语言函数来快速实现一个简单的音频流媒体服务,以便您能更好地理解和使用这一功能。第一步:准备工作首先,您需要安装Go语言的开发环境。您可以从官方网站(https://golan

快速入门:使用Go语言函数实现简单的图像识别功能在如今的科技发展中,图像识别技术已经成为一个热门的话题。作为一种快速高效的编程语言,Go语言具备了实现图像识别功能的能力。本文将通过使用Go语言函数实现简单的图像识别功能,给读者提供一个快速入门的指南。首先,我们需要安装Go语言的开发环境。可以在Go语言官方网站(https://golang.org/)上下载适

Title:快速上手:五款Go语言常用框架推荐近年来,随着Go语言的流行,越来越多的开发者选择采用Go进行项目开发。Go语言以其高效、简洁和性能优越等特点受到了广泛关注。在Go语言开发中,选择适合的框架能够提高开发效率和代码质量。本文将介绍五款Go语言常用框架,并附上代码示例,帮助读者快速上手。Gin框架Gin是一个轻量级的web框架,具有快速高效的特点,

快速入门:五种Kafka可视化工具的使用指南1.Kafka监控工具:简介ApacheKafka是一种分布式发布-订阅消息系统,它可以处理大量的数据,并提供高吞吐量和低延迟。由于Kafka的复杂性,需要使用可视化工具来帮助监控和管理Kafka集群。2.Kafka可视化工具:五大选择KafkaManager:KafkaManager是一个开源的Web界

快速入门:使用Go语言函数实现简单的数据可视化折线图展示引言:在数据分析和可视化的领域中,折线图是一种常用的图表类型,可以清晰地展示数据随时间或其他变量的变化趋势。本文将介绍如何使用Go语言函数来实现一个简单的数据可视化折线图展示,并且提供相关的代码实例。一、准备工作在开始之前,需要确保以下几个条件:安装Go语言环境,并设置好相关的环境变量。安装必要的依赖库

快速入门:使用Go语言函数实现简单的消息推送功能在当今移动互联网时代,消息推送已成为各种APP的标配功能。Go语言是一门快速高效的编程语言,非常适合用来开发消息推送功能。本文将介绍如何使用Go语言函数实现简单的消息推送功能,并提供相应的代码示例,帮助读者快速入门。在开始之前,我们需要了解一下消息推送的基本原理。通常,消息推送功能需要两个主要的组件:推送服务器


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Dreamweaver CS6
Visual web development tools

ZendStudio 13.5.1 Mac
Powerful PHP integrated development environment

Safe Exam Browser
Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

PhpStorm Mac version
The latest (2018.2.1) professional PHP integrated development tool
