Search
  • +44-7459919437 (UK- WhatsApp & Direct Call) | +91-6383544892 (India - WhatsApp Only) | Email Id : vinothrwins@gmail.com
Vinoth Tech Solutions
  • Home
  • Selenium Java Online Training
  • Self Paced Video Course
    • Selenium Course Curriculum
    • Cypress Course Curriculum
    • Playwright Course Curriculum
  • Tutorials
  • Demo Sites
    • E-Commerce Demo Application
    • Practice Automation
      • Demo Page Healthcare
      • Registration Form
      • Transaction Details
      • DropDown
      • Mouse Event
      • Keyboard Events
      • Alert and Popup
      • Multiple Windows
      • iFrames
      • Wait WebElement
      • WebTable
  • FAQS
  • About Me & Feedback
    • Placed Students Feedback
    • Online Training Feedback
    • LinkedIn Profile
    • TechTalk
  • Free YouTube Courses
    • Python for Automation
    • Free QA Video Courses
      • Manual Testing
      • Java For Automation
      • Selenium Webdriver
      • TestNG
      • Cucumber BDD
      • UFT(QTP) Automation
    • Free Data Science Courses
      • Artificial Intelligence for Beginners
      • Python For A.I
      • Python Pandas
      • Python NumPy
      • Mathematics for A.I
  • Home
  • Selenium Java Online Training
  • Self Paced Video Course
    • Selenium Course Curriculum
    • Cypress Course Curriculum
    • Playwright Course Curriculum
  • Tutorials
  • Demo Sites
    • E-Commerce Demo Application
    • Practice Automation
      • Demo Page Healthcare
      • Registration Form
      • Transaction Details
      • DropDown
      • Mouse Event
      • Keyboard Events
      • Alert and Popup
      • Multiple Windows
      • iFrames
      • Wait WebElement
      • WebTable
  • FAQS
  • About Me & Feedback
    • Placed Students Feedback
    • Online Training Feedback
    • LinkedIn Profile
    • TechTalk
  • Free YouTube Courses
    • Python for Automation
    • Free QA Video Courses
      • Manual Testing
      • Java For Automation
      • Selenium Webdriver
      • TestNG
      • Cucumber BDD
      • UFT(QTP) Automation
    • Free Data Science Courses
      • Artificial Intelligence for Beginners
      • Python For A.I
      • Python Pandas
      • Python NumPy
      • Mathematics for A.I

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:

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.
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 Vinoth Tech Solutions Ltd (UK), Reg. No: 16489105