---
title: How to handle cookies in selenium
description: "**How to handle cookies in selenium** Selenium WebDriver provides full support for working with cookies. You can add, retrieve, delete, and manage cookies during your automation session. **Real-World Usage Examples** - **Session Handling:** Store session cookies to bypass login on…"
url: "https://vinothqaacademy.com/docs/how-to-handle-cookies-in-selenium/"
updated: "2025-09-26"
category: Selenium Automation Testing
tags:
  - Selenium Automation Testing
---

# How to handle cookies in selenium

**How to handle cookies in selenium**

Selenium WebDriver provides full support for working with cookies. You can add, retrieve, delete, and manage cookies during your automation session.

**Real-World Usage Examples**

- **Session Handling:** Store session cookies to bypass login on subsequent runs.

- **Cookie-based Authentication:** Inject a cookie after login, or set pre-authenticated cookies for test setup.

- **Testing Cookie-related Features:** Add/modify cookies to test how your app behaves.

Sample Program:

```javascript

                import org.openqa.selenium.WebDriver;

import org.openqa.selenium.chrome.ChromeDriver;

import org.openqa.selenium.Cookie;

import java.util.Set;

public class CookiesDemo {

    public static void main(String[] args) {

        System.setProperty("webdriver.chrome.driver", "/path/to/chromedriver");

        WebDriver driver = new ChromeDriver();

        try {

            driver.get("https://example.com");

            // Add a cookie

            Cookie cookie = new Cookie("testCookie", "Selenium123");

            driver.manage().addCookie(cookie);

            // Get all cookies

            Set<Cookie> allCookies = driver.manage().getCookies();

            System.out.println("All Cookies:");

            for (Cookie c : allCookies) {

                System.out.println(c.getName() + " = " + c.getValue());

            }

            // Get a specific cookie

            Cookie myCookie = driver.manage().getCookieNamed("testCookie");

            System.out.println("testCookie value: " + myCookie.getValue());

            // Delete a cookie

            driver.manage().deleteCookieNamed("testCookie");

            // Delete all cookies

            driver.manage().deleteAllCookies();

        } finally {

            driver.quit();

        }

    }

}
```

**Tips**

- Cookies are domain-specific: Add them after navigating to the correct domain!

- Can set additional properties: domain, path, expiry, secure, etc.

```javascript

                Cookie customCookie = new Cookie.Builder("name", "value")

    .domain("example.com")

    .path("/")

    .isHttpOnly(true)

    .build();
```
