Handling Cookies in Webdriver

Selenium webdriver can perform required task with respect to browser cookies. We add , delete, delete particular cookie by passing the name, and so on. Let us look all of the in details.

First let us see how add cookie works:

Add Cookie
Method Name: addCookie(Cookie cookie)
Syntax:driver.manage().addCookie(arg0);
Purpose: To add a specific cookie into cookies. If the cookie's domain name is left blank, it is assumed that the cookie is meant for the domain of the current document.
Parameters: cookie - The name of the cookie to add.
Click here for example program

Delete Cookie
Method Name: deleteCookie(Cookie cookie)
Syntax: driver.manage().deleteCookie(arg0);
Purpose: Delete a cookie from the browser's "cookie jar". The domain of the cookie will be ignored.
Parameter: Cookie
Click here for example program

Delete Cookie with Name
Method Name: deleteCookieNamed(java.lang.String name)
Syntax: driver.manage().deleteCookieNamed(arg0);
Purpose: Delete the named cookie from the current domain. This is equivalent to setting the named cookie's expiry date to sometime in the past.
Parameters: name - The name of the cookie to delete
Click here for example program

Delete All Cookies
Method Name: deleteAllCookies()
Syntax: driver.manage().deleteAllCookies();
Purpose: It will delete all the cookies for the current domain.
Parameters: N/A
Click here for example program

Get Cookies
Method Name: getCookies()
Syntax: driver.manage().getCookies();
Purpose: Get all the cookies for the current domain. This is the equivalent of calling "document.cookie" and parsing the result.
Returns: A Set of cookies for the current domain.

Get the Cookie with Specific Name
Method Name: getCookieNamed(java.lang.String name)
Syntax: driver.manage().getCookieNamed(arg0);
Purpose: To Get a cookie with a given name.
Parameters: name - the name of the cookie
Returns: It will return the cookie value for the name specified, or null if no cookie found with the given name

Selenium Tutorials: 

Comments

Hi, please let us know, how to access a cookie which was set in the previous session and login to current session based on information from the previous session

Add new comment

CAPTCHA
This question is for testing whether or not you are a human visitor and to prevent automated spam submissions.