How to Handle Cookies in Selenium

 

Cookies are used to track user in website. A cookie is a small piece of data sent from a website and stored on the user’s computer. Cookies also recognize users returning to a website and loading the previously stored information. It is used to store the user’s identity and track the user’s journey through the website’s pages.

In Selenium WebDriver (Java), cookies represent small pieces of data stored in the browser by the website. These can include session IDs, user preferences, authentication tokens, etc.


👉 Selenium allows you to:

  • Get all cookies

  • Add a cookie

  • Delete a cookie

  • Delete all cookies


👉 Cookies are especially useful for:

  • Maintaining sessions between tests

  • Simulating logged-in users without going through login pages

  • Testing scenarios involving cookies


👉 Cookie Methods in Selenium Java

Selenium provides cookie handling through the WebDriver.Options interface via driver.manage().getCookies() and related methods.


MethodDescription
driver.manage().getCookies()Returns a set of all cookies
driver.manage().getCookieNamed("name")Returns a specific cookie by name
driver.manage().addCookie(Cookie cookie)Adds a new cookie
driver.manage().deleteCookie(cookie)Deletes a specific cookie
driver.manage().deleteCookieNamed("name")Deletes a cookie by name
driver.manage().deleteAllCookies()Deletes all cookies


How to add a cookie in Selenium

Below is the sample code to add a cookie by Selenium java:


package com.cookies;

import java.util.concurrent.TimeUnit;
import org.openqa.selenium.Cookie;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;

public class AddCookie {
    
    static String projectLocation = System.getProperty("user.dir");

    public static void main(String[] args) throws InterruptedException {

        System.setProperty("webdriver.chrome.driver", projectLocation + "/driver/chromedriver.exe");

        WebDriver driver = new ChromeDriver();
        
        // maximize window
        driver.manage().window().maximize();

        // delete all cookies
        driver.manage().deleteAllCookies();

        // dynamic wait
        driver.manage().timeouts().pageLoadTimeout(50, TimeUnit.SECONDS);
        driver.manage().timeouts().implicitlyWait(50, TimeUnit.SECONDS);
        
        driver.get("https://www.google.com");
        
        Cookie cookie = new Cookie("newCookie","goformule");
        driver.manage().addCookie(cookie);
        
        Thread.sleep(2000);
        driver.quit();
        
    }
}


How to get a cookie in Selenium:

Below is the sample code to get a cookie by Selenium java:



package com.cookies;

import java.util.concurrent.TimeUnit;
import org.openqa.selenium.Cookie;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;

public class GetCookie {

    static String projectLocation = System.getProperty("user.dir");

    public static void main(String[] args) throws InterruptedException {

        System.setProperty("webdriver.chrome.driver", projectLocation + "/driver/chromedriver.exe");

        WebDriver driver = new ChromeDriver();

        // maximize window
        driver.manage().window().maximize();

        // delete all cookies
        driver.manage().deleteAllCookies();

        // dynamic wait
        driver.manage().timeouts().pageLoadTimeout(50, TimeUnit.SECONDS);
        driver.manage().timeouts().implicitlyWait(50, TimeUnit.SECONDS);

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

        Cookie cookie = new Cookie("newCookie", "goformule");
        driver.manage().addCookie(cookie);
        
        Cookie cookieName = driver.manage().getCookieNamed("newCookie");
        System.out.println("Cookie Name: "+cookieName.getValue());
        
        Thread.sleep(2000);
        driver.quit();
    }

}

Output:

Cookie Name: tutorials123



How to get all cookies in Selenium:

Below is the sample code to get all cookies by Selenium java:

package com.cookies;

import java.util.Iterator;
import java.util.Set;
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.Cookie;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;

public class GetAllCookies {
    
    static String projectLocation = System.getProperty("user.dir");

    public static void main(String[] args) throws InterruptedException {

        System.setProperty("webdriver.chrome.driver", projectLocation + "/driver/chromedriver.exe");

        WebDriver driver = new ChromeDriver();

        // maximize window
        driver.manage().window().maximize();

        // delete all cookies
        driver.manage().deleteAllCookies();

        // dynamic wait
        driver.manage().timeouts().pageLoadTimeout(50, TimeUnit.SECONDS);
        driver.manage().timeouts().implicitlyWait(50, TimeUnit.SECONDS);

        driver.get("https://www.google.com");
        
        Set<Cookie> set = driver.manage().getCookies();
        
        Iterator<Cookie> it = set.iterator();
        while(it.hasNext()) {
            System.out.println(it.next());
        }
        
        Thread.sleep(2000);
        driver.quit();
    }

}

How to delete a cookie in Selenium

Below is the sample code to delete a cookie by Selenium java:


package com.cookies;

import java.util.concurrent.TimeUnit;
import org.openqa.selenium.Cookie;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;

public class DeleteCookie {
    
    static String projectLocation = System.getProperty("user.dir");

    public static void main(String[] args) throws InterruptedException {

        System.setProperty("webdriver.chrome.driver", projectLocation + "/driver/chromedriver.exe");

        WebDriver driver = new ChromeDriver();

        // maximize window
        driver.manage().window().maximize();

        // delete all cookies
        driver.manage().deleteAllCookies();

        // dynamic wait
        driver.manage().timeouts().pageLoadTimeout(50, TimeUnit.SECONDS);
        driver.manage().timeouts().implicitlyWait(50, TimeUnit.SECONDS);

        driver.get("https://www.google.com");
        
        Cookie cookie = new Cookie("newCookie","goformule");
        driver.manage().addCookie(cookie);

        driver.manage().deleteCookie(cookie);
        System.out.println("Cookies Deleted");

        Thread.sleep(2000);
        driver.quit();
    }
    



}

Output:

Cookies Deleted


How to delete a cookie by name in Selenium

Below is the sample code to delete a cookie by name by Selenium java:



package com.cookies;

import java.util.concurrent.TimeUnit;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;

public class DeleteCookieByName {
    
    static String projectLocation = System.getProperty("user.dir");

    public static void main(String[] args) throws InterruptedException {

        System.setProperty("webdriver.chrome.driver", projectLocation + "/driver/chromedriver.exe");

        WebDriver driver = new ChromeDriver();

        // maximize window
        driver.manage().window().maximize();

        // delete all cookies
        driver.manage().deleteAllCookies();

        // dynamic wait
        driver.manage().timeouts().pageLoadTimeout(50, TimeUnit.SECONDS);
        driver.manage().timeouts().implicitlyWait(50, TimeUnit.SECONDS);

        driver.get("https://www.google.com");
        
        driver.manage().deleteCookieNamed("goformule");
        System.out.println("Cookies Deleted");

        Thread.sleep(2000);
        driver.quit();
    }



}

Output:

Cookies Deleted


How to delete all cookies in Selenium

Below is the sample code to delete all cookies by Selenium java:


package com.cookies;

import java.util.concurrent.TimeUnit;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;

public class DeleteAllCookies {

    static String projectLocation = System.getProperty("user.dir");

    public static void main(String[] args) throws InterruptedException {

        System.setProperty("webdriver.chrome.driver", projectLocation + "/driver/chromedriver.exe");

        WebDriver driver = new ChromeDriver();

        // maximize window
        driver.manage().window().maximize();

        // delete all cookies
        driver.manage().deleteAllCookies();

        // dynamic wait
        driver.manage().timeouts().pageLoadTimeout(50, TimeUnit.SECONDS);
        driver.manage().timeouts().implicitlyWait(50, TimeUnit.SECONDS);

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

        driver.manage().deleteAllCookies();
        System.out.println("Cookies Deleted");

        Thread.sleep(2000);
        driver.quit();
    }
}

Output:

Cookies Deleted

No comments:

Post a Comment