We use cookies to analyze site traffic and show personalized ads. You can accept all cookies or decline personalized advertising.
Learn more in our Privacy Policy.
Manage browser cookies to test authentication, sessions, and preferences.
Cookies store session data, authentication tokens, and user preferences. Selenium lets you add, read, and delete cookies to simulate different user states without logging in repeatedly.
getCookies(): Get all cookies for the domain.getCookieNamed(name): Get a specific cookie.addCookie(cookie): Add a new cookie.deleteCookieNamed(name): Remove a cookie.deleteAllCookies(): Clear all cookies.driver.get() before adding cookies.Use Selenium to add test cookies, then reload to verify they appear!
12345678910111213141516171819202122// 1. Print initial cookies
for (Cookie c : driver.manage().getCookies()) {
System.out.println("Cookie -> " + c.getName() + ": " + c.getValue());
}
// 2. Add sample cookies
Cookie cookie1 = new Cookie("test1", "cookie1");
driver.manage().addCookie(cookie1);
// 3. Refresh so the page can read the new cookies
driver.navigate().refresh();
// 4. Click the "Reload Cookie Status" button in the UI
driver.findElement(By.id("reloadCookieStatusBtn")).click();
// 5. Delete one cookie by name
driver.manage().deleteCookieNamed("test1");
// 6. Delete all cookies
driver.manage().deleteAllCookies();
driver.quit();Set<Cookie> for all cookies on the current domain.Always open the target domain with driver.get() before adding cookies—WebDriver won't accept cookies for a different host.
After adding or deleting cookies, use refresh() so the application can read the latest cookie state.
Use deleteAllCookies() at the start or end of your tests to avoid "dirty" sessions leaking between test cases.