Skip to content

WebDevHubs

  • Home
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • Java
  • Selenium
  • PHP
  • Python
  • Programs
  • Toggle search form

How to Delete Particular Cookie using Selenium Java?

Posted on July 20, 2025July 20, 2025 By Admin No Comments on How to Delete Particular Cookie using Selenium Java?

To delete a particular (specific) cookie in Selenium Java, you have two main approaches:

1. Delete by Cookie Name

Use the deleteCookieNamed(String name) method, passing the exact name of the cookie you wish to remove. This is the simplest and most common way.

Example:

// Delete cookie with the name "session_id"
driver.manage().deleteCookieNamed("session_id");
  • The string must match the cookie’s name exactly. To check available cookie names, you can use driver.manage().getCookies() and print their names.

2. Delete by Cookie Object

If you have a Cookie object (for example, retrieved via getCookieNamed()), use the deleteCookie(Cookie cookie) method:

import org.openqa.selenium.Cookie;

// Get the cookie object by name
Cookie cookie = driver.manage().getCookieNamed("session_id");
if (cookie != null) {
    driver.manage().deleteCookie(cookie);
}
  • This approach is useful if you need to match a cookie not just by name but also by path, domain, or other attributes.

Complete Code

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.Cookie;

public class DeleteSpecificCookie {
    public static void main(String[] args) {
        WebDriver driver = new ChromeDriver();
        driver.get("https://www.example.com");

        // Delete by name:
        driver.manage().deleteCookieNamed("session_id");

        // Or delete by Cookie object:
        Cookie cookie = driver.manage().getCookieNamed("user_token");
        if (cookie != null) {
            driver.manage().deleteCookie(cookie);
        }

        driver.quit();
    }
}

Key Points:

  • The name you provide must be the exact name of the cookie.
  • These methods affect only cookies for the current domain loaded in the browser.
  • Use deleteCookieNamed() for convenience, or deleteCookie() with a Cookie object for finer control.

This allows for precise, selective removal of cookies instead of clearing all cookies at once.

Selenium Tags:Selenium-Java

Post navigation

Previous Post: How to Maximize the Window Size in Selenium Java?
Next Post: How to Navigate to a Particular URL in Selenium Java?

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Categories

  • CSS
  • HTML
  • Interview Experience
  • Java
  • JavaScript
  • Lodash
  • PHP
  • Programs
  • Python
  • Selenium
  • Software Testing
  • Web Technologies
  • Web Templates

Recent Posts

  • Java ArrayList trimToSize() Method
  • Java ArrayList toArray() Method
  • Java ArrayList subList() Method
  • Java ArrayList spliterator() Method
  • Java ArrayList sort() Method

Recent Comments

No comments to show.

Important Pages

  • About Us
  • Contact Us
  • Terms of Use
  • Privacy Policy

Web Development

  • HTML
  • CSS
  • JavaScript
  • PHP

Programming Languages

  • Java
  • Python
  • PHP
  • Programs

Others

  • Selenium
  • Lodash
  • Java ArrayList
  • JavaScript Array Methods

Copyright © 2025 WebDevHubs.

Powered by PressBook Green WordPress theme