Selenium Java - Add Cookies to Browser

Selenium Java - Add Cookies to Browser
Introduction
In this blog, we'll walk you through using Selenium WebDriver with Java to open a website and add browser cookies with the addCookie()
method. We'll be using Selenium 4.6+, which comes with Selenium Manager for automatic driver management, so you won't have to worry about manual driver setup or WebDriverManager.
Sample Selenium Code
Here is a complete code using ChromeDriver to open a website, add cookies, and retrieve them:
package sa.Demo.SeleniumPractice;
import org.openqa.selenium.Cookie;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.testng.annotations.Test;
import java.time.Duration;
public class Selenium_012 {
/****
* Sample Code Definition :- Selenium Test - Browser - Add Cookies
* Developed By :- Pankaj Kumar
* Date of Creation :- 11-June-2024
* Project :- SAJVSAUTO - Java [Web / Mobile / API]
* SA_BLOG_CODE :- SAJVSAUTO_012
*/
@Test
public void Selenium_012() throws InterruptedException {
System.out.println("Selenium Test - Browser - Add Cookies");
// Open Chrome Browser
WebDriver driver = new ChromeDriver();
// Maximize the Browser Window
driver.manage().window().maximize();
// Set an implicit wait of 30 seconds
driver.manage().timeouts().implicitlyWait(Duration.ofSeconds(30));
// Navigate to the practice website
driver.get("https://practice.solveautomation.in/");
// Wait for 5 seconds to ensure the website is fully loaded
Thread.sleep(5000);
// Add cookies to the browser
Cookie newCookie = new Cookie("AutomationName", "SolveAutomation");
driver.manage().addCookie(newCookie);
// Print all cookies present in the browser
System.out.println("All Cookies: " + driver.manage().getCookies());
// Retrieve and print the value of the specific cookie
System.out.println("Value of Cookie ['AutomationName']: " + driver.manage().getCookieNamed("AutomationName").getValue());
// Wait for 5 seconds to observe the output
Thread.sleep(5000);
// Quit the Driver to close all browser windows and end the session
driver.quit();
}
}
Explanation
ChromeDriver Initialization: Launches a new Chrome browser.
Window Management: Maximizes the browser window for better visibility.
Implicit Wait: Configures a timeout for locating elements.
driver.get(): Opens the specified URL in the browser.
addCookie(): Adds a new cookie to the current domain.
getCookies(): Retrieves all cookies associated with the domain.
getCookieNamed(): Retrieves a specific cookie by name and prints its value.
Thread Sleep: Adds a delay to observe the browser and output (for demo purposes).
driver.quit(): Closes all browser windows and ends the WebDriver session.
Conclusion
This guide shows you how to use Selenium WebDriver with Java to open a website, add cookies, and get cookie values using Selenium's cookie management API.
Subscribe to my newsletter
Read articles from Pankaj Kumar directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by
