PhantomJS openUrl()
openUrl方法打开一个网页。它类似于phantomjs的open方法。这个方法有一些额外的参数,它们是 httpConf, settings 和 callback 函数。
HttpConf
HttpConf是一个具有以下属性的对象:
- Operation - 它是http方法GET/POST
-
Data - 它用于POST方法。
-
Headers - 类似于wpage.customHeaders的对象。
httpConf的默认值是 get 方法。它是可选的,您可以指定null。
Settings
它类似于wpage.settings属性。如果不想指定相同的值,可以使用null。
Callback
当页面加载完成时调用。
语法
语法如下:
wpage = openUrl(url, httpConf, settings);
示例
以下示例展示了 openUrl() 方法的使用。
var wPage = require('webpage').create();
wPage.settings.userAgent = 'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36
(KHTML, like Gecko) Chrome/37.0.2062.120 Safari/537.36';
wPage.onLoadFinished = function(){
console.log('Entering on load finished');
console.log(wPage.content);
console.log(JSON.stringify(wPage.settings));
}
wPage.openUrl("http://localhost/tasks/a.html","POST", wPage.settings);
上述程序生成以下 输出 。
Entering on load finished
<html>
<head>
<title>Welcome to phantomjs test page</title>
</head>
<body name = "a">
<script type = "text/javascsript">
window.onload = function() {
window.open("http://localhost/tasks/alert.html", "t1");
}
</script>
<h1>This is a test page</h1>
<h1>This is a test page</h1>
<h1>This is a test page</h1>
<h1>This is a test page</h1>
<h1>This is a test page</h1>
<h1>This is a test page</h1>
<h1>This is a test page</h1>
<h1>This is a test page</h1>
<h1>This is a test page</h1>
</body>
</html>
{"XSSAuditingEnabled":false,"javascriptCanCloseWindows":true,"javascriptCanOpen
Windows":true,"javascriptEnabled":true,"loadImages":true,"localToRemoteUrlAccess
Enabled":false,"userAgent":"Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36
(KHTML, like Gecko) Chrome/37.0.2062.120 Safari/537.36","webSecurityEnabled":true}