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

相关文章

Python使用date模块进行日期处理的终极指南

《Python使用date模块进行日期处理的终极指南》在处理与时间相关的数据时,Python的date模块是开发者最趁手的工具之一,本文将用通俗的语言,结合真实案例,带您掌握date模块的六大核心功能... 目录引言一、date模块的核心功能1.1 日期表示1.2 日期计算1.3 日期比较二、六大常用方法详

浅析CSS 中z - index属性的作用及在什么情况下会失效

《浅析CSS中z-index属性的作用及在什么情况下会失效》z-index属性用于控制元素的堆叠顺序,值越大,元素越显示在上层,它需要元素具有定位属性(如relative、absolute、fi... 目录1. z-index 属性的作用2. z-index 失效的情况2.1 元素没有定位属性2.2 元素处

python中time模块的常用方法及应用详解

《python中time模块的常用方法及应用详解》在Python开发中,时间处理是绕不开的刚需场景,从性能计时到定时任务,从日志记录到数据同步,时间模块始终是开发者最得力的工具之一,本文将通过真实案例... 目录一、时间基石:time.time()典型场景:程序性能分析进阶技巧:结合上下文管理器实现自动计时

HTML5 data-*自定义数据属性的示例代码

《HTML5data-*自定义数据属性的示例代码》HTML5的自定义数据属性(data-*)提供了一种标准化的方法在HTML元素上存储额外信息,可以通过JavaScript访问、修改和在CSS中使用... 目录引言基本概念使用自定义数据属性1. 在 html 中定义2. 通过 JavaScript 访问3.

CSS模拟 html 的 title 属性(鼠标悬浮显示提示文字效果)

《CSS模拟html的title属性(鼠标悬浮显示提示文字效果)》:本文主要介绍了如何使用CSS模拟HTML的title属性,通过鼠标悬浮显示提示文字效果,通过设置`.tipBox`和`.tipBox.tipContent`的样式,实现了提示内容的隐藏和显示,详细内容请阅读本文,希望能对你有所帮助... 效

解读为什么@Autowired在属性上被警告,在setter方法上不被警告问题

《解读为什么@Autowired在属性上被警告,在setter方法上不被警告问题》在Spring开发中,@Autowired注解常用于实现依赖注入,它可以应用于类的属性、构造器或setter方法上,然... 目录1. 为什么 @Autowired 在属性上被警告?1.1 隐式依赖注入1.2 IDE 的警告:

Node.js net模块的使用示例

《Node.jsnet模块的使用示例》本文主要介绍了Node.jsnet模块的使用示例,net模块支持TCP通信,处理TCP连接和数据传输,具有一定的参考价值,感兴趣的可以了解一下... 目录简介引入 net 模块核心概念TCP (传输控制协议)Socket服务器TCP 服务器创建基本服务器服务器配置选项服

HTML5中下拉框<select>标签的属性和样式详解

《HTML5中下拉框<select>标签的属性和样式详解》在HTML5中,下拉框(select标签)作为表单的重要组成部分,为用户提供了一个从预定义选项中选择值的方式,本文将深入探讨select标签的... 在html5中,下拉框(<select>标签)作为表单的重要组成部分,为用户提供了一个从预定义选项中

在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