Selenium: dealing with cookies

Last updated on:

Cookies are usually small text files, given ID tags that are stored on your computer's browser directory or program data subfolders. Cookies are created when you use your browser to visit a website that uses cookies to keep track of your movements within the site, help you resume where you left off, remember your registered login, theme selection, preferences, and other customization functions.The website stores a corresponding file(with same ID tag)to the one they set in your browser and in this file they can track and keep information on your movements within the site and any information you may have voluntarily given while visiting the website, such as email address. [Original source]

Here is a screenshot how you can inspect cookies in Chrome developer tool:

How to delete cookie on with webdriver?

 // delete all cookie
 driver.manage().deleteAllCookies();

 Cookie cookie = driver.manage().getCookieNamed("SomeCookieName");
 driver.manage().deleteCookie(cookie);

 // delete cookie named
 driver.manage().deleteCookieNamed("");

How to get cookie so you can assert it later on

// Get cookie named
 Cookie cookie = driver.manage().getCookieNamed("SomeCookieName");

// Get all cookies
driver.manage().getCookies();

How to send cookies with selenium webdriver?

Cookie cookie = new Cookie("name", "value");
driver.manage().addCookie(cookie);

For more information An interface for managing stuff you would do in a browser menu

View Comments

Leave a Comment

Use markdown or basic HTML and be nice.