phontomjs webpage模块属性

2024-05-07 02:08

本文主要是介绍phontomjs webpage模块属性,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

随时随地技术实战干货,获取项目源码、学习资料,请关注源代码社区公众号(ydmsq666)

canGoBack

Examples

var webPage = require('webpage');
var page = webPage.create();
// @TODO: Finish page.canGoBack example.

canGoForward

Examples

var webPage = require('webpage');
var page = webPage.create();
// @TODO: Finish page.canGoForward example.

clipRect

clipRect {object}

This property defines the rectangular area of the web page to be rasterized when page.render is invoked. If no clipping rectangle is set, page.render will process the entire web page.

Examples

var webPage = require('webpage');
var page = webPage.create();page.clipRect = {top: 14,left: 3,width: 400,height: 300
};

Issues

clipRect doesn’t seem to be applied for JPEG render exports, works with PNG though.

指定截图的区域,若没有指定默认整个页面。

content

content {string}

This property stores the content of the web page (main frame), enclosed in an HTML/XML element. Setting the property will effectively reload the web page with the new content.

See also page.plainText to get the content without any HTML tags.

Examples

var webPage = require('webpage');
var page = webPage.create();page.open('http://phantomjs.org', function (status) {var content = page.content;console.log('Content: ' + content);phantom.exit();
});

cookies

Get or set Cookies visible to the current URL (though, for setting, use of page.addCookieis preferred). This array will be pre-populated by any existing Cookie data visible to this URL that is stored in the CookieJar, if any.

Cookies is an array of objects:

{ domain: 'example.com',expires: 'Sat Oct 11 2014 21:44:33 GMT+0200 (CEST)',expiry: 1476128618,httponly: false,name: 'cookieName',path: '/',secure: false,value: cookieValue
}

Examples

var webPage = require('webpage');
var page = webPage.create();page.open('http://phantomjs.org', function (status) {var cookies = page.cookies;console.log('Listing cookies:');for(var i in cookies) {console.log(cookies[i].name + '=' + cookies[i].value);}phantom.exit();
});

customHeaders

customHeaders {object}

Introduced: PhantomJS 1.5

This property specifies additional HTTP request headers that will be sent to the server for every request issued (for pages and resources). The default value is an empty object {}. Headers names and values get encoded in US-ASCII before being sent. Please note that the ‘User-Agent’ should be set using the page.settings, setting the ‘User-Agent’ property in this property will overwrite the value set via page.settings.

Examples

Send two additional headers ‘X-Test’ and ‘DNT’

var webPage = require('webpage');
var page = webPage.create();page.customHeaders = {"X-Test": "foo","DNT": "1"
};

Do you only want these customHeaders passed to the initial page.open request?

Here’s the recommended workaround:

var webPage = require('webpage');
var page = webPage.create();page.customHeaders = {"X-Test": "foo","DNT": "1"
};page.onInitialized = function() {page.customHeaders = {};
};

event

Examples

var webPage = require('webpage');
var page = webPage.create();
// @TODO: Finish page.event example.

focusedFrameName

Examples

var webPage = require('webpage');
var page = webPage.create();
// @TODO: Finish page.focusedFrameName example.

content {string}

This property stores the content of the web page (main frame), enclosed in an HTML/XML element. Setting the property will effectively reload the web page with the new content.

See also page.plainText to get the content without any HTML tags.

Examples

var webPage = require('webpage');
var page = webPage.create();page.open('http://phantomjs.org', function (status) {var content = page.content;console.log('Content: ' + content);phantom.exit();
});

frameContent

frameContent {string}

Introduced: PhantomJS 1.7

This property stores the content of the web page’s currently active frame (which may or may not be the main frame), enclosed in an HTML/XML element.

Setting the property will effectively reload the web page with the new content.

Examples

var webPage = require('webpage');
var page = webPage.create();
// @TODO: Finish page.frameContent example.

frameName

Examples

var webPage = require('webpage');
var page = webPage.create();
// @TODO: Finish page.frameName example.

framePlainText

framePlainText {string}

Introduced: PhantomJS 1.7

Read-only. This property stores the content of the web page’s currently active frame (which may or may not be the main frame) as plain text — no element tags!

Examples

var webPage = require('webpage');
var page = webPage.create();
// @TODO: Finish page.framePlainText example.

frameTitle

Examples

var webPage = require('webpage');
var page = webPage.create();
// @TODO: Finish page.frameTitle example.

frameUrl

frameUrl {string}

Introduced: PhantomJS 1.7

Read-only. This property gets the current URL of the web page’s currently active frame (which may or may not be the main frame).

Examples

var webPage = require('webpage');
var page = webPage.create();
// @TODO: Finish page.frameUrl example.

framesCount

Examples

var webPage = require('webpage');
var page = webPage.create();
// @TODO: Finish page.framesCount example.

framesName

Returns array with frames names.

Examples

var webPage = require('webpage');
var page = webPage.create();
// @TODO: Finish page.framesName example.

libraryPath

libraryPath {string}

This property stores the path which is used by page.injectJs function to resolve the script name.

Initially it is set to the location of the script invoked by PhantomJS.

Examples

var webPage = require('webpage');
var page = webPage.create();
// @TODO: Finish page.libraryPath example.

navigationLocked

navigationLocked {boolean}

This property defines whether navigation away from the page is permitted or not. If it is set to true, then the page is locked to the current URL. Defaults to false.

Examples

var webPage = require('webpage');
var page = webPage.create();
// @TODO: Finish page.navigationLocked example.

设置为true后,该页面锁定为当前url。

offlineStoragePath

Examples

var webPage = require('webpage');
var page = webPage.create();
// @TODO: Finish page.offlineStoragePath example.

ownsPages

Examples

var webPage = require('webpage');
var page = webPage.create();
// @TODO: Finish page.ownsPages example.

pagesWindowName

Examples

var webPage = require('webpage');
var page = webPage.create();
// @TODO: Finish page.pagesWindowName example.

pages

Examples

var webPage = require('webpage');
var page = webPage.create();
// @TODO: Finish page.pages example.

paperSize

paperSize {object}

This property defines the size of the web page when rendered as a PDF.

Values and measurements

paperSize

If no paperSize is defined, the size is defined by the web page.

Supported dimension units are: 'mm''cm''in''px'. No unit means 'px'.

Margin

(Previously known as Border, which can still be used for compatibility purposes)

Margin is optional and defaults to 0. Can be provided as a single dimension or as an object containing one or more of the following properties: 'top''left''bottom''right'. Margin values cannot be negative.

Format

Supported formats are: 'A3''A4''A5''Legal''Letter''Tabloid'.

Orientation

Orientation ('portrait''landscape') is optional and defaults to 'portrait'.

Headers and Footers

A repeating page header and footer can also be added via the header and footer property. These can be provided as an object that contains a height and a contents property. The contents property must be set as a phantom.callback object.

header: {height: "1.2cm",contents: phantom.callback(function(pageNum, numPages) {return "<h1>Header <span style='float:right'>" + pageNum + " / " + numPages + "</span></h1>";})
}

Acceptable Objects

The given object should be in either this format:

{width: '200px',height: '300px',margin: '0px'
}

or this format:

{format: 'A4',orientation: 'portrait',margin: '1cm'
}

Examples

var webPage = require('webpage');
var page = webPage.create();page.paperSize = {width: '5in',height: '7in',margin: {top: '50px',left: '20px'}
};

A repeating page header and footer can also be added via this property, as in this example:

var webPage = require('webpage');
var page = webPage.create();page.paperSize = {width: '8.5in',height: '11in',header: {height: "1cm",contents: phantom.callback(function(pageNum, numPages) {return "<h1>Header <span style='float:right'>" + pageNum + " / " + numPages + "</span></h1>";})},footer: {height: "1cm",contents: phantom.callback(function(pageNum, numPages) {return "<h1>Footer <span style='float:right'>" + pageNum + " / " + numPages + "</span></h1>";})}
}

plainText

plainText {string}

Read-only. This property stores the content of the web page (main frame) as plain text — no element tags!

See also: page.content which returns the content with element tags.

Examples

var webPage = require('webpage');
var page = webPage.create();page.open('http://phantomjs.org', function (status) {console.log('Stripped down page text:\n' + page.plainText);phantom.exit();
});

scrollPosition

scrollPosition {object}

This property defines the scroll position of the web page.

Examples

var webPage = require('webpage');
var page = webPage.create();page.scrollPosition = {top: 100,left: 0
};

settings

settings {object}

This property stores various settings of the web page:

  • javascriptEnabled defines whether to execute the script in the page or not (defaults to true).
  • loadImages defines whether to load the inlined images or not (defaults to true).
  • localToRemoteUrlAccessEnabled defines whether local resource (e.g. from file) can access remote URLs or not (defaults to false).
  • userAgent defines the user agent sent to server when the web page requests resources.
  • userName sets the user name used for HTTP authentication.
  • password sets the password used for HTTP authentication.
  • XSSAuditingEnabled defines whether load requests should be monitored for cross-site scripting attempts (defaults to false).
  • webSecurityEnabled defines whether web security should be enabled or not (defaults to true).
  • resourceTimeout (in milli-secs) defines the timeout after which any resource requested will stop trying and proceed with other parts of the page. onResourceTimeout callback will be called on timeout.

Note: The settings apply only during the initial call to the page.open function. Subsequent modification of the settings object will not have any impact.

Examples

var webPage = require('webpage');
var page = webPage.create();
page.settings.userAgent = 'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/37.0.2062.120 Safari/537.36';

title

Examples

page.open('http://phantomjs.org', function (status) {console.log(page.title); // get page Titlephantom.exit();
});

url

url {string}

Introduced: PhantomJS 1.7 Read-only. This property gets the current URL of the web page (main frame).

Examples

var webPage = require('webpage');
var page = webPage.create();page.open('http://phantomjs.org', function (status) {var url = page.url;console.log('URL: ' + url);phantom.exit();
});

viewportSize

viewportSize {object}

This property sets the size of the viewport for the layout process. It is useful to set the preferred initial size before loading the page, e.g. to choose between 'landscape' vs 'portrait'.

Because PhantomJS is headless (nothing is shown), viewportSize effectively simulates the size of the window like in a traditional browser.

Examples

var webPage = require('webpage');
var page = webPage.create();page.viewportSize = {width: 480,height: 800
};

You must include height for this to work.

var page = require('webpage').create(),address, output, size;
var system = require('system');	var arg_count = system.args.length - 1;
if (arg_count < 4 || arg_count > 5) {console.log('Usage: viewport.js URL filename sizeX sizeY');phantom.exit();
} else {address = system.args[1];output = system.args[2];sizeX = system.args[3];sizeY = system.args[4];page.viewportSize = { width: sizeX, height: sizeY };page.open(address, function (status) {if (status !== 'success') {console.log('Unable to load the address!');} else {window.setTimeout(function () {page.render(output);phantom.exit();}, 200);}});
}

Usage: viewport.js URL filename sizeX sizeY

windowName

Examples

var webPage = require('webpage');
var page = webPage.create();
// @TODO: Finish page.windowName example.

zoomFactor

zoomFactor {number}

This property specifies the scaling factor for the page.render and page.renderBase64functions.

The default is 1, i.e. 100% zoom.

Examples

Create a thumbnail preview with 25% zoom

var webPage = require('webpage');
var page = webPage.create();page.zoomFactor = 0.25;
page.render('capture.png');

这篇关于phontomjs webpage模块属性的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/966031

相关文章

在Java中使用ModelMapper简化Shapefile属性转JavaBean实战过程

《在Java中使用ModelMapper简化Shapefile属性转JavaBean实战过程》本文介绍了在Java中使用ModelMapper库简化Shapefile属性转JavaBean的过程,对比... 目录前言一、原始的处理办法1、使用Set方法来转换2、使用构造方法转换二、基于ModelMapper

Python利用自带模块实现屏幕像素高效操作

《Python利用自带模块实现屏幕像素高效操作》这篇文章主要为大家详细介绍了Python如何利用自带模块实现屏幕像素高效操作,文中的示例代码讲解详,感兴趣的小伙伴可以跟随小编一起学习一下... 目录1、获取屏幕放缩比例2、获取屏幕指定坐标处像素颜色3、一个简单的使用案例4、总结1、获取屏幕放缩比例from

nginx-rtmp-module模块实现视频点播的示例代码

《nginx-rtmp-module模块实现视频点播的示例代码》本文主要介绍了nginx-rtmp-module模块实现视频点播,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习... 目录预置条件Nginx点播基本配置点播远程文件指定多个播放位置参考预置条件配置点播服务器 192.

JavaScript中的isTrusted属性及其应用场景详解

《JavaScript中的isTrusted属性及其应用场景详解》在现代Web开发中,JavaScript是构建交互式应用的核心语言,随着前端技术的不断发展,开发者需要处理越来越多的复杂场景,例如事件... 目录引言一、问题背景二、isTrusted 属性的来源与作用1. isTrusted 的定义2. 为

Java如何通过反射机制获取数据类对象的属性及方法

《Java如何通过反射机制获取数据类对象的属性及方法》文章介绍了如何使用Java反射机制获取类对象的所有属性及其对应的get、set方法,以及如何通过反射机制实现类对象的实例化,感兴趣的朋友跟随小编一... 目录一、通过反射机制获取类对象的所有属性以及相应的get、set方法1.遍历类对象的所有属性2.获取

多模块的springboot项目发布指定模块的脚本方式

《多模块的springboot项目发布指定模块的脚本方式》该文章主要介绍了如何在多模块的SpringBoot项目中发布指定模块的脚本,作者原先的脚本会清理并编译所有模块,导致发布时间过长,通过简化脚本... 目录多模块的springboot项目发布指定模块的脚本1、不计成本地全部发布2、指定模块发布总结多模

Python中构建终端应用界面利器Blessed模块的使用

《Python中构建终端应用界面利器Blessed模块的使用》Blessed库作为一个轻量级且功能强大的解决方案,开始在开发者中赢得口碑,今天,我们就一起来探索一下它是如何让终端UI开发变得轻松而高... 目录一、安装与配置:简单、快速、无障碍二、基本功能:从彩色文本到动态交互1. 显示基本内容2. 创建链

Node.js 中 http 模块的深度剖析与实战应用小结

《Node.js中http模块的深度剖析与实战应用小结》本文详细介绍了Node.js中的http模块,从创建HTTP服务器、处理请求与响应,到获取请求参数,每个环节都通过代码示例进行解析,旨在帮... 目录Node.js 中 http 模块的深度剖析与实战应用一、引言二、创建 HTTP 服务器:基石搭建(一

python中的与时间相关的模块应用场景分析

《python中的与时间相关的模块应用场景分析》本文介绍了Python中与时间相关的几个重要模块:`time`、`datetime`、`calendar`、`timeit`、`pytz`和`dateu... 目录1. time 模块2. datetime 模块3. calendar 模块4. timeit

Python模块导入的几种方法实现

《Python模块导入的几种方法实现》本文主要介绍了Python模块导入的几种方法实现,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学... 目录一、什么是模块?二、模块导入的基本方法1. 使用import整个模块2.使用from ... i