- PhantomJS Tutorial
- PhantomJS - Home
- PhantomJS - Overview
- PhantomJS - Environment Setup
- PhantomJS - Object
- PhantomJS - Methods
- WebPage Module
- PhantomJS - Properties
- PhantomJS - Methods
- PhantomJS - Events/Callbacks
- PhantomJS - Child Process Module
- File System Module
- PhantomJS - Properties
- PhantomJS - Methods
- System Module
- PhantomJS - Properties
- Web Server Module
- PhantomJS - Properties
- PhantomJS - Methods
- Miscellaneous
- Command Line Interface
- PhantomJS - Screen Capture
- PhantomJS - Page Automation
- PhantomJS - Network Monitoring
- PhantomJS - Testing
- PhantomJS - REPL
- PhantomJS - Examples
- PhantomJS Useful Resources
- PhantomJS - Quick Guide
- PhantomJS - Useful Resources
- PhantomJS - Discussion
PhantomJS - addCookie()
The addCookie method adds cookies to the page specified. For the cookie to be added, the domain name has to match the page otherwise, the cookie is ignored. It returns true, if added successfully otherwise false. The Name, Value and Domain are mandatory fields in the addcookie method.
Right now, we will add cookies to the page a.html. Therefore, wpage.cookies will give the newly added cookie and the existing cookies present on page a.html.
Syntax
Its syntax is as follows −
phantom.addCookie({ 'name' : 'cookie1', /* mandatory property */ 'value' : '1234', /* mandatory property */ 'domain' : 'localhost', /* mandatory property */ 'path' : '/', 'httponly' : true, 'secure' : false, 'expires' : (new Date()).getTime() + (5000 * 60 * 60) });
Example
Let us look at an example of the addCookie () method.
var wpage = require('webpage').create(); phantom.addCookie ({ 'name' : 'cookie1', /* mandatory property */ 'value' : '1234', /* mandatory property */ 'domain' : 'localhost', /* mandatory property */ 'path' : '/', 'httponly' : true, 'secure' : false, 'expires' : (new Date()).getTime() + (5000 * 60 * 60) }); wpage.open ('http://localhost/tasks/a.html', function() { console.log(JSON.stringify(wpage.cookies)); phantom.exit(); });
The above program generates the following output.
[{"domain":".localhost","expires":"Sun, 07 May 2017 01:13:45 GMT","expiry":1494 99825,"httponly":true,"name":"cookie1","path":"/","secure":false,"value":"1234" ,{"domain":"localhost","expires":"Fri, 22 Dec 2017 12:00:00 GMT","expiry":15139 4000,"httponly":false,"name":"username","path":"/tasks/","secure":false,"value" "Roy"}]
phantomjs_webpage_module_methods.htm
Advertisements