Delete Cookies On All Domains using Selenium Webdriver.


We can delete cookies on all domains with Selenium. The method deleteAllCookies is used to delete all cookies from the present domain. First, we shall add cookies, then get them and finally delete all the cookies.

Syntax

driver.manage().deleteAllCookies();

Example

import java.util.Set;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import java.util.concurrent.TimeUnit;
public class DeleteCookies{
   public static void main(String[] args) {
      System.setProperty("webdriver.chrome.driver",
      "C:\Users\ghs6kor\Desktop\Java\chromedriver.exe");
      WebDriver driver = new ChromeDriver();
      driver.get("https://www.tutorialspoint.com/index.htm");
      // wait of 4 seconds
      driver.manage().timeouts().implicitlyWait(4, TimeUnit.SECONDS);
      // setting name and value for cookie
      Cookie c = new Cookie("test", "selenium");
      Cookie r = new Cookie("subject", "Java");
      // cookie addition
      driver.manage().addCookie(c);
      driver.manage().addCookie(r);
      // obtain the cookies
      Set ck = driver.manage().getCookies();
      System.out.println("Cookie count: "+ck.size());
      // delete cookies
      driver.manage().deleteAllCookies();
      // obtain the cookies after delete
      Set ch = driver.manage().getCookies();
      System.out.println("Cookie count after delete: "+ch.size());
   }
}

Output

Updated on: 30-Nov-2020

2K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements