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
- Add the cookie with
driver.manage().addCookie()and a newCookie. - Read it back with
getCookieNamed()and print itsgetValue()after the labelCookie value:. - Delete it with
deleteCookieNamed(). - Call
getCookieNamed()again and print whether the result isnullafter the labelCookie 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 returnsnull, sogetValue()throwsNullPointerException. Test the result fornullinstead.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.
Related lessons
- Navigate Between PagesLoad a page first so the cookie has a domain.
- JavaScript Executor - Get ValuesRead
document.cookiefrom JavaScript to see what the page sees. - Get Page SourceRead the page's HTML, another way to inspect what the browser holds.
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.
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();
}
}