Managing Cookies in Selenium with Java, JavaScript, and Python

🔍 Introduction

When automating web applications using Selenium, managing browser cookies is crucial for testing user sessions, authentication, and personalization. Whether you’re using Java, Python, or JavaScript, Selenium provides methods to get, add, and delete cookies easily.

In this blog post, we’ll walk through how to manage cookies in Selenium across three popular programming languages — Java, JavaScript, and Python — with practical examples.


✅ What Are Cookies in Web Automation?

Cookies are small pieces of data stored in a user’s browser. They’re often used for:

  • User session management (e.g., login sessions)
  • Tracking user preferences
  • Feature flags
  • Authentication handling

For automation testers, managing cookies can be useful for:

  • Skipping login screens
  • Verifying session timeouts
  • Testing across user roles

// Get all cookies
Set<Cookie> cookies = driver.manage().getCookies();

// Get a specific cookie by name
Cookie sessionCookie = driver.manage().getCookieNamed("session_id");

// Add a cookie
Cookie newCookie = new Cookie("test_cookie", "test_value");
driver.manage().addCookie(newCookie);

// Delete a specific cookie
driver.manage().deleteCookieNamed("test_cookie");

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

💡 Pro Tip: Always add cookies after navigating to the site using driver.get("https://your-site.com").


# Get all cookies
cookies = driver.get_cookies()

# Get a specific cookie
cookie = driver.get_cookie("session_id")

# Add a new cookie
driver.add_cookie({"name": "test_cookie", "value": "test_value"})

# Delete a specific cookie
driver.delete_cookie("test_cookie")

# Delete all cookies
driver.delete_all_cookies()

Use Case: You can save session cookies after login and reuse them in another test run to avoid repeated logins.


// Get all cookies
let cookies = await driver.manage().getCookies();

// Get a specific cookie
let cookie = await driver.manage().getCookie("session_id");

// Add a cookie
await driver.manage().addCookie({ name: "test_cookie", value: "test_value" });

// Delete a cookie
await driver.manage().deleteCookie("test_cookie");

// Delete all cookies
await driver.manage().deleteAllCookies();

Remember: Use await with these methods since Selenium WebDriver JavaScript API is Promise-based.


  • Reuse Login Sessions: Store cookies after a successful login and load them later to skip login screens.
  • Test Expired Sessions: Manually expire session cookies and test logout behavior.
  • A/B Testing & Feature Flags: Set specific cookies to simulate different user experiments.

⚠️ Common Pitfalls to Avoid

  • Adding cookies before opening a page will fail — always navigate to a domain before adding cookies.
  • Domain mismatch between the cookie and current URL can cause silent failures.
  • Some secure cookies (e.g., HttpOnly) cannot be modified from Selenium.

📌 Conclusion

Managing cookies in Selenium is a powerful way to simulate real-world scenarios like login, logout, session expiry, and user tracking. By mastering cookie handling in Java, Python, and JavaScript, you can build more robust and efficient test cases.


Previous Article

8 Naughty Tricks Girls Can Use to Drive Men Wild with Desire

Next Article

selenium-grid4-remotewebdriver-spurious-exception

Write a Comment

Leave a Comment

Your email address will not be published. Required fields are marked *

Subscribe to our Newsletter

Subscribe to our email newsletter to get the latest posts delivered right to your email.
Pure inspiration, zero spam ✨