Table of contents
1.
Introduction
2.
Prerequisites
3.
A brief about Cookies
4.
Importance of Clearing Cookies
5.
Methods to Clear Cookies in Selenium WebDriver
5.1.
Method 1: Using deleteCookieNamed()
5.2.
Java
5.3.
Method 2: Using deleteCookie()
5.4.
Java
5.5.
Method 3: Using deleteAllCookies()
5.6.
Java
6.
Frequently Asked Questions
6.1.
If we clear cookies in Selenium WebDriver, will it impact other tests? 
6.2.
Is there any way to clear cookies for specific domains? 
6.3.
Does clearing cookies log us out of the website? 
6.4.
Can we clear cookies while a test is running? 
7.
Conclusion
Last Updated: Mar 27, 2024
Medium

How to Clear Cookies in Selenium WebDriver?

Career growth poll
Do you think IIT Guwahati certified course can help you in your career?

Introduction

Cookies play a crucial role in web applications by storing user-specific information, session data, and other essential details. However, during web automation testing with Selenium WebDriver, cookies can sometimes interfere with test scenarios. To ensure accurate and reliable testing, it's essential to know how to clear cookies when necessary.

how to clear cookies in selenium webdriver

In this article, we will discuss how to clear cookies in Selenium WebDriver.

So, let's get started!!

Prerequisites

If we want to clear cookies in Selenium WebDriver, we need to have two things that are: 

  • Selenium WebDriver
     
  • Web browser 


So, Selenium WebDriver is a widely used test automation framework. It is an open-source framework that makes it easy to use for everyone. It is primarily used for automating web browsers. It provides various tools and libraries. That’s why it enables you to automate web applications across different browsers. It is supported by multiple programming languages(Java, Python, C#, etc.). We can use a web browser, such as Chrome, Edge, etc., to clear the cookies in Selenium WebDriver.

A brief about Cookies

When we go to a website, some kind of data is stored on our device, and this small piece of data is known as cookies. This data consists of information like passwords or preferences for a certain website. Cookies are kind of text files that have some data or information about a user. For example, it can also have a user’s browsing history. 

When a user visits a website, the server can send cookies to the user's browser, which then stores them locally. 

Cookies help us in various things:

  • Session Management
     
  • Personalization
     
  • Remembering the users
     
  • Security 
     
  • Authentication
     
  • Tracking the user
     

**Now, you might be wondering why we need to clear cookies in Selenium WebDriver.

Importance of Clearing Cookies

There are several reasons to clear the cookies in Selenium WebDriver:

  • If we clear cookies, we can ensure that each test scenario starts with a clean slate. It will eliminate all the pre-existing user sessions or preferences
     
  • If we clear cookies, it will help us in obtaining accurate testing outcomes. If we have some unused or remaining cookies, then it can lead us to unexpected behavior
     
  • If we clear cookies, we can simulate a new user's experience. We can also validate whether the application performs as expected
     
  • If we clear cookies, we can maintain data privacy. As we know, cookies can contain the data of the user; that's why if we clear the cookies, then we can ensure the privacy of the user
     

**Now you may have a doubt about how to clear cookies in Selenium WebDriver. Let us understand methods to clear the cookies.

Methods to Clear Cookies in Selenium WebDriver

When it comes to clearing cookies in Selenium WebDriver, there are three types of methods that we can use to clear the cookies:

Method 1: Using deleteCookieNamed()

If we want to clear a specific named cookie, then we can use deleteCookieNamed() method. 

Considering the Coding Ninjas website, suppose we want to delete a cookie. 

Let us discuss it with the help of an example:

  • Java

Java

package com.selenium.test.codingninjas;

import org.openqa.selenium.Cookie;

import org.openqa.selenium.WebDriver;

import org.openqa.selenium.edge.EdgeDriver;



public class DeleteNameCookies{



public static void main(String[] args) {

// Set the path of the MS Edge browser

       System.setProperty("webdriver.edge.driver", "C:\\Users\\naray\\Downloads\\edgedriver_win64\\msedgedriver.exe");

      

       // Initialize the WebDriver with EdgeDriver

       WebDriver driver = new EdgeDriver();

      

       // Navigating to Coding Ninjas Website

       driver.get("https://www.codingninjas.com");

      

       // Finding a Cookie and note the name of the cookie

       System.out.println("Cookies before deletion:");

       for (Cookie ck : driver.manage().getCookies()) {

           System.out.println(ck.getName() + " - " + ck.getValue());

       }


      // Deleting a cookie

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


      // Printing the list of all cookies after deletion

      System.out.println("\nAll the Cookies after deletion:");

      for (Cookie ck : driver.manage().getCookies()) {

   System.out.println(ck.getName() + " - " + ck.getValue());

      }


       // Closing the browser

       driver.quit();

}



}
You can also try this code with Online Java Compiler
Run Code


Output:

output


Explanation

In this example, firstly, we have created a class called DeleteNameCookies. Then we set the property of our Edge driver, and then we used that Edge driver to get information about the Coding Ninjas website. Then we have to find all the cookies related to the website by using the method  driver.manage().getCookies() and note down a single cookie. We can also use pen and paper to note down the name of the cookie to delete. Then we have used the deleteCookieNamed() method to delete the _vwo_uuid_v2 cookie. Lastly, we have printed all the rest cookies that were left.

Method 2: Using deleteCookie()

If we want to clear a specific cookie object, then we can use deleteCookieNamed() method. Considering the Amazon website, suppose we want to delete the session-id-time cookie

Let us discuss it with the help of an example:

  • Java

Java

package com.selenium.test.codingninjas;

import org.openqa.selenium.Cookie;

import org.openqa.selenium.WebDriver;

import org.openqa.selenium.edge.EdgeDriver;



public class DeleteCookie {



public static void main(String[] args) {

// Set the path of the MS Edge browser

       System.setProperty("webdriver.edge.driver", "C:\\Users\\naray\\Downloads\\edgedriver_win64\\msedgedriver.exe");

      

       // Initialize the WebDriver with EdgeDriver

       WebDriver driver = new EdgeDriver();

      

       // Navigating to Amazon Website

       driver.get("https://www.amazon.in/");

      

       // Finding a Cookie and note the name of the cookie

       System.out.println("Cookies before deletion:");

       for (Cookie ck : driver.manage().getCookies()) {

           System.out.println(ck.getName() + " - " + ck.getValue());

       }    

       // Creating a Cookie object and getting the Cookie by name

       Cookie deleteCk = driver.manage().getCookieNamed("session-id-time");


       // Checking if selected Cookie is null or not

       if (deleteCk != null) {

           // Deleting the cookie

           driver.manage().deleteCookie(deleteCk);

           System.out.println("\nCookie with the name 'session-id-time' is deleted.");

       } else {

           System.out.println("\nCookie with the name 'session-id-time' not found.");

       }



       // Printing the list of cookies after deletion

       System.out.println("\nCookies after deletion:");

       for (Cookie ck : driver.manage().getCookies()) {

           System.out.println(ck.getName() + " - " + ck.getValue());

       }



       // Close the browser

       driver.quit();


}


}
You can also try this code with Online Java Compiler
Run Code


Output:

output

Explanation

In this example, firstly, we have created a class called DeleteCookie. Then we set the property of our Edge driver, and then we used that Edge driver to get information about the Amazon website. Then we have to find all the cookies related to the website by using the method  driver.manage().getCookies() and note down a single cookie. We can also use pen and paper to note down the name of the cookie to delete. Then we created an object of Cookie and stored the name of the cookie, i.e., session-id-time. Then we used the deleteCookie() method to delete the cookie. Lastly, we have printed all the rest of the cookies that were left.

Method 3: Using deleteAllCookies()

If we want to clear all the cookies, then we can use deleteAllCookies() method. Considering the Flipkart website, suppose we want to delete all the cookies. 

Let us discuss it with the help of an example:

  • Java

Java

package com.selenium.test.codingninjas;

import org.openqa.selenium.Cookie;

import org.openqa.selenium.WebDriver;

import org.openqa.selenium.edge.EdgeDriver;


public class DeleteAllCookies {



public static void main(String[] args) {

// Set the path of the MS Edge browser

       System.setProperty("webdriver.edge.driver", "C:\\Users\\naray\\Downloads\\edgedriver_win64\\msedgedriver.exe");

      

       // Initialize the WebDriver with EdgeDriver

       WebDriver driver = new EdgeDriver();

      

       // Navigating to Flipkart Website

       driver.get("https://www.flipkart.com/");

      

       // Printing all the Cookies before deletion

       System.out.println("Cookies before deletion:");

       for (Cookie ck : driver.manage().getCookies()) {

           System.out.println(ck.getName() + " - " + ck.getValue());

       }

      

       // Delete all the cookies

       driver.manage().deleteAllCookies();

       System.out.println("\n!!Cookies Deleted!!");

      

       // Printing all the Cookies after deletion

       int count=0;

       System.out.print("\nCookies after deletion:");

       for (Cookie ck : driver.manage().getCookies()) {

           System.out.println(ck.getName() + " - " + ck.getValue());

           count++;

       }

       System.out.println("No Cookies Left!!");

}

}
You can also try this code with Online Java Compiler
Run Code


Output:

output

Explanation

In this example, firstly, we have created a class called DeleteAllCookies. Then we set the property of our Edge driver, and then we used that Edge driver to get information about the Flipkart website. Then we printed all the cookies before deletion related to the website by using the method  driver.manage().getCookies(). Then we used the deleteAllCookies() method to delete all the cookies. Lastly, we have printed all the rest of the cookies that were left.

Frequently Asked Questions

If we clear cookies in Selenium WebDriver, will it impact other tests? 

If we clear cookies, it will only affect the current session. All the other tests run in separate sessions, so they won't be impacted by clearing cookies.

Is there any way to clear cookies for specific domains? 

We can use the delete_all_cookies() method to clear cookies for the current domain. If we want to clear cookies for a specific domain, then we can navigate to that domain using WebDriver, and then we can clear cookies.

Does clearing cookies log us out of the website? 

Yes, clearing cookies logs us out of the website if the website relies on cookies for user sessions.

Can we clear cookies while a test is running? 

Yes, we can call the cookie-clearing methods, such as delete_all_cookies(), while a test is running. We can clear cookies at any point during the test execution.

Conclusion

In this article, we have discussed how to clear cookies in Selenium WebDriver. We have also explained the methods to clear the cookies with the help of an example. If we clear cookies regularly, then we can ensure accurate and reliable test results. If you want to learn more about Selenium, then you can check out our other articles:

We hope this article helped you to get knowledge about how to clear cookies in Selenium WebDriver. You can refer to our guided paths on the Coding Ninjas Studio platform. You can check our course to learn more about DSADBMSCompetitive ProgrammingPythonJavaJavaScript, etc. 

To practice and improve yourself in the interview, you can also check out Interview ExperienceCoding interview questions, and the Ultimate Guide path for interviews.

Happy Coding!!

Live masterclass