PhantomJS - cookies Property



We have cookies property on the phantom object as well as on the phantom webpage object. With cookies, you can set /get the cookies available on the URL. It will also give you the cookies available on the URL and the new cookies set on that page.

Syntax

Its syntax is as follows −

page.cookies;

Example

Take a look at the following example to understand how to use the cookies property.

var wpage = require('webpage').create(); 
wpage.open('http://localhost/tasks/a.html', function (status) { 
   var cookies = wpage.cookies;   
   console.log('Cookies available on page are as follows :'); 
   console.log(JSON.stringify(cookies));   
   phantom.exit(); 
});

The above program generates the following output.

Cookies available on page are as follows : 
[{"domain":"localhost","expires":"Fri, 22 Dec 2017 12:00:00 GMT","expiry":151394 
4000,"httponly":false,"name":"username","path":"/tasks/","secure":false,"value" : 
"Roy"}] 

If you check the page.content example, we have set the cookie to the page using document.cookie = "username = Roy; expires = Thu, 22 Dec 2017 12:00:00 UTC";

When we try to read the cookies of the page, it lists out all the details of a cookie, such as its Domain, Expires, Httponly, Name, Value, Path, etc. The page.cookies returns all the cookies available on a page.

phantomjs_webpage_module_properties.htm
Advertisements