Skip to content
CodeKruPro

Handle Cookies

68/70

Handle Cookies

Selenium can manage browser cookies:

  • manage().addCookie(new Cookie("name", "value"))
  • manage().getCookieNamed("name")
  • manage().getCookies() - get all cookies
  • manage().deleteCookieNamed("name")
  • manage().deleteAllCookies()

Task:

  1. Navigate to https://testkru.com/Elements/TextFields
  2. Add a cookie named "testCookie" with value "SeleniumJava"
  3. Retrieve and print the cookie value
  4. Delete the cookie and verify it's gone

Expected output:

Cookie value: SeleniumJava
Cookie deleted: true

About this lesson

Add, get and delete cookies in Selenium Java

Learn Selenium JavaAdvancedAdvanced Topics

How it works

Cookies are managed through driver.manage(): addCookie(new Cookie("theme", "dark")) adds one, getCookieNamed("theme") returns a Cookie or null, getCookies() returns all of them as a Set, and deleteCookieNamed() and deleteAllCookies() remove them.

Cookies belong to a domain, so the browser must already be on a page of that site. Adding one in a fresh session, still on data:,, throws InvalidCookieDomainException, and naming another domain throws the same exception with "Cookie 'domain' mismatch".

The site sets cookies of its own. On Text Fields, getCookies() already contains two analytics cookies before you add anything, so count only the cookies you care about. A cookie you add without an expiry is a session cookie: getExpiry() returns null, and it survives a page reload.

About the assignment

On Text Fields you add a cookie with the name and value the task gives, read it back by name, then delete it and prove it is gone.

What you will do

  1. Add the cookie with driver.manage().addCookie() and a new Cookie.
  2. Read it back with getCookieNamed() and print its getValue() after the label Cookie value:.
  3. Delete it with deleteCookieNamed().
  4. Call getCookieNamed() again and print whether the result is null after the label Cookie deleted:.

Common mistakes

  • Adding the cookie before loading a page

    There is no domain to attach it to, so Chrome refuses with InvalidCookieDomainException.

  • Calling getValue() after deleting

    getCookieNamed() now returns null, so getValue() throws NullPointerException. Test the result for null instead.

  • Putting a semicolon in the value

    A semicolon ends a cookie value, and Chrome rejects it with UnableToSetCookieException. Encode such values first.

In a real test suite

Cookie control speeds up suites: tests log in once through the API, add the session cookie to the browser and skip the login page. Tests also delete cookies to check logged-out behaviour or a consent banner on a first visit.

Lesson 67 read table data. Lesson 69 goes back to waits for content that changes while you watch it.

To try one of these calls on any page without a task, open the Online Selenium Java Compiler.

Starter code

The editor above opens with this code. Fill in the TODO lines, then run and submit it.

Main.java
import org.openqa.selenium.*;
import org.openqa.selenium.chrome.ChromeDriver;

public class Main {
    public static void main(String[] args) throws Exception {
        WebDriver driver = new ChromeDriver();
        driver.get("https://testkru.com/Elements/TextFields");
        
        // TODO: Add a cookie named "testCookie" with value "SeleniumJava"
        // TODO: Get the cookie and print its value
        // TODO: Delete the cookie and verify it's null
        
        driver.quit();
    }
}