PhantomJS - openUrl() 方法

openUrl 方法打开一个网页,和phantomjs的open方法类似,该方法还有一些额外的参数,分别是httpConf、settingscallback函数。

HttpConf

HttpConf是一个对象,有以下属性 −

  • Operation − 是http方法GET/POST

  • Data − 用于POST方法。

  • Headers − 一个像wpage.customHeaders的对象。

httpConf的默认是get方法。它是可选的,您可以为其指定 null。

设置

它类似于 wpage.settings 属性。如果您不想指定它,可以使用 null。

回调

它在页面加载时被调用。

语法

其语法如下 −

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} 

phantomjs_webpage_module_methods.html