PhantomJS - openUrl()



The openUrl method opens up a webpage. It is similar to open method of phantomjs. This method has some additional parameters, which are httpConf, settings and callback functions.

HttpConf

HttpConf is an object that has the following properties −

  • Operation − It is the http method GET/POST

  • Data − It is used for POST method.

  • Headers − An object like wpage.customHeaders.

The default for httpConf is the get method. It is optional and you can specify null for the same.

Settings

It is similar to wpage.settings property. You can use null, if you do not want to specify the same.

Callback

It is called when a page is loaded.

Syntax

Its syntax is as follows −

wpage = openUrl(url, httpConf, settings); 

Example

The following example shows the use of openUrl() method.

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); 

The above program generates the following output.

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.htm
Advertisements