Search
  • +44-7459919437 (UK- WhatsApp & Direct Call) | +91-6383544892 (India - WhatsApp Only) | Email Id : vinothtechsolutions@gmail.com
Vinoth Tech Solutions
  • Home
  • Tutorials
  • End-to-End QA Projects
    • API Manual and Automation Testing using SoapUI
    • DevOps CI CD using Real Time Project Setup
  • Self Paced Video Course
    • Selenium Course Curriculum
    • Cypress Course Curriculum
    • Playwright Course Curriculum
  • About Me & Feedback
    • LinkedIn Profile
    • Placed Students Feedback
    • Online Training Feedback
    • TechTalk
  • FAQS
  • Demo Sites
    • Practice Automation
      • Registration Form
      • Transaction Details
      • DropDown
      • Basic Authentication
      • Mouse Event
      • Keyboard Events
      • Alert and Popup
      • Multiple Windows
      • iFrames
      • Wait WebElement
      • WebTable
      • Date Picker Calendar
    • E-Commerce Demo Application
    • Healthcare Demo Page
    • Create Account Demo Page
    • Python JS Online Compiler
  • Home
  • Tutorials
  • End-to-End QA Projects
    • API Manual and Automation Testing using SoapUI
    • DevOps CI CD using Real Time Project Setup
  • Self Paced Video Course
    • Selenium Course Curriculum
    • Cypress Course Curriculum
    • Playwright Course Curriculum
  • About Me & Feedback
    • LinkedIn Profile
    • Placed Students Feedback
    • Online Training Feedback
    • TechTalk
  • FAQS
  • Demo Sites
    • Practice Automation
      • Registration Form
      • Transaction Details
      • DropDown
      • Basic Authentication
      • Mouse Event
      • Keyboard Events
      • Alert and Popup
      • Multiple Windows
      • iFrames
      • Wait WebElement
      • WebTable
      • Date Picker Calendar
    • E-Commerce Demo Application
    • Healthcare Demo Page
    • Create Account Demo Page
    • Python JS Online Compiler

Selenium Automation Testing

  • Introduction to Automation Testing
  • Introduction to Selenium
  • Advantages and Disadvantages of Selenium WebDriver
  • Locators in Selenium
  • How to launch the browsers ? 
  • Browser Navigation Commands in Selenium
  • Handling the textbox and button using selenium
  • Handling the radio button and checkbox using selenium
  • Handling the drop downs and multi-select dropdown
  • Validate the text validation and attributes values. 
  • Handling Images
  • Handling Mouse Events
  • Handling Keyboard Events
  • Handling Alert Popup
  • Handling iFrames
  • Handling Multiple Browsers or Tabs
  • Types of wait statement in selenium
  • How to take screenshots?
  • How to upload File in Selenium?
  • How to handle scroll function using JavaScriptExecutor?
  • How to handle basic functionality using JavaScriptExecutor?
  • How to handle advanced functionality using JavaScriptExecutor?
  • How to automate CAPTCHA?
  • How to handle cookies in selenium
View Categories
  • Home
  • Tutorials
  • Selenium Automation
  • Selenium Automation Testing
  • How to handle cookies in selenium

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:

📄
filename.js
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.
📄
filename.js
Cookie customCookie = new Cookie.Builder("name", "value")

    .domain("example.com")

    .path("/")

    .isHttpOnly(true)

    .build();
Selenium Automation Testing
What are your Feelings

Share This Article :

  • Facebook
  • X
  • LinkedIn
How to automate CAPTCHA?
© 2018 – 2025 V-Tech Solutions Ltd (UK), Reg. No: 16489105