Introduction
Reading Cookies in Selenium allows you to retrieve cookies stored in the browser during an automation session. Cookies contain valuable information such as user sessions, authentication tokens, language preferences, and other website-specific data.
Selenium provides two methods for reading cookies:
get_cookie(name)– Retrieves a specific cookie by its name.get_cookies()– Retrieves all cookies stored for the current website.
Reading cookies helps automation engineers verify session information, validate authentication, inspect browser data, and debug cookie-related issues.
In this tutorial, you will learn what reading cookies means, why it is used, Selenium cookie reading methods, practical examples, real-world applications, common mistakes, best practices, and frequently asked interview questions.
What is Reading Cookies?
Reading cookies means retrieving cookie information that is already stored in the browser.
Selenium can:
Read a single cookie.
Read all cookies.
Access cookie properties such as name, value, domain, expiry, path, and security settings.
Cookies can be inspected without interacting with the website’s user interface.
Why Do We Read Cookies?
Reading cookies is commonly used to:
Verify login sessions.
Validate authentication tokens.
Inspect browser session data.
Debug cookie-related issues.
Confirm that cookies were added successfully.
Verify website behavior after login.
Selenium Methods for Reading Cookies
| Method | Description |
|---|---|
get_cookie(name) | Returns a specific cookie by its name. |
get_cookies() | Returns all cookies for the current domain. |
Syntax
Read a Specific Cookie
driver.get_cookie("cookie_name")
Read All Cookies
driver.get_cookies()
Practical Example
The following example demonstrates how to read both a single cookie and all cookies from the browser.
The automation script opens the Selenium practice website, adds a sample cookie, retrieves that specific cookie, retrieves all cookies, and verifies that the cookie exists.
from selenium import webdriver
# Topic: 9. Managing Browser Sessions - Reading Cookies
# Practice site: https://the-internet.herokuapp.com/
#
# get_cookie(name) returns one cookie and get_cookies() returns every cookie
# for the current domain.
def test_read_cookies():
driver = webdriver.Chrome()
try:
driver.get("https://the-internet.herokuapp.com/")
driver.add_cookie(
{
"name": "session_id",
"value": "abc123",
}
)
single_cookie = driver.get_cookie("session_id")
all_cookies = driver.get_cookies()
assert single_cookie["name"] == "session_id"
assert any(
cookie["name"] == "session_id"
for cookie in all_cookies
)
finally:
driver.quit()
Output
Chrome launched successfully.
Website opened successfully.
Cookie added successfully.
Retrieved Single Cookie:
Name: session_id
Value: abc123
Retrieved All Cookies.
Assertion Passed.
Test Executed Successfully.
Understanding the Code
Import WebDriver
from selenium import webdriver
Imports the Selenium WebDriver module.
Launch Chrome
driver = webdriver.Chrome()
Starts a new Chrome browser session.
Open the Website
driver.get("https://the-internet.herokuapp.com/")
Navigates to the Selenium practice website.
Add a Sample Cookie
driver.add_cookie(
{
"name": "session_id",
"value": "abc123",
}
)
Creates a sample cookie so it can be read later in the test.
Read a Single Cookie
single_cookie = driver.get_cookie("session_id")
Retrieves only the cookie named session_id.
Read All Cookies
all_cookies = driver.get_cookies()
Returns every cookie stored for the current website as a list.
Verify the Cookie
assert single_cookie["name"] == "session_id"
Verifies that the retrieved cookie has the expected name.
Verify the Cookie Exists in the Cookie List
assert any(
cookie["name"] == "session_id"
for cookie in all_cookies
)
Checks whether the cookie exists in the complete list of browser cookies.
Close the Browser
driver.quit()
Closes all browser windows and ends the WebDriver session.
Browser Execution Flow
Launch Chrome
│
▼
Open Website
│
▼
Add Sample Cookie
│
▼
Read Specific Cookie
│
▼
Read All Cookies
│
▼
Verify Cookie Exists
│
▼
Assertions Pass
│
▼
Close Browser
Information Available in a Cookie
Each Selenium cookie is stored as a Python dictionary.
Common properties include:
| Property | Description |
|---|---|
name | Cookie name |
value | Cookie value |
domain | Website domain |
path | Cookie path |
expiry | Cookie expiration time |
secure | HTTPS-only cookie |
httpOnly | Prevents JavaScript access |
Automation Testing Example
Suppose your application stores an authentication cookie after a successful login.
Your Selenium automation:
Logs into the application.
Reads the authentication cookie.
Verifies that the cookie exists.
Confirms that the user session has been created successfully.
This helps validate session management functionality.
Real-World Example
Consider an online banking application.
After users log in, the application creates a secure session cookie.
During automation:
Selenium reads the session cookie.
Verifies that authentication succeeded.
Confirms that the cookie contains the expected session information.
Continues with authenticated test scenarios.
This helps ensure that login functionality is working correctly.
Common Mistakes Beginners Make
Trying to Read Cookies Before Opening a Website
Incorrect
driver = webdriver.Chrome()
driver.get_cookie("session_id")
No website has been opened, so no cookies are available.
Correct
driver.get("https://the-internet.herokuapp.com/")
driver.get_cookie("session_id")
Expecting a Cookie That Doesn’t Exist
If the requested cookie is not present, get_cookie() returns None.
Always ensure the cookie exists before accessing its properties.
Confusing get_cookie() with get_cookies()
get_cookie(name)returns one specific cookie.get_cookies()returns all cookies for the current domain.
Best Practices
Open the target website before reading cookies.
Verify that cookies exist before using them.
Use
get_cookie()when retrieving a specific cookie.Use
get_cookies()when inspecting all browser cookies.Validate authentication cookies during login tests.
Always close the browser using
driver.quit().
Conclusion
Reading Cookies allows Selenium to retrieve browser cookies for validation, debugging, and session management. Using get_cookie() and get_cookies(), automation engineers can inspect browser state, verify authentication, and ensure cookies are created correctly during test execution.
Understanding how to read cookies is an important part of browser session management in Selenium automation.
Frequently Asked Questions (FAQs)
What is get_cookie() in Selenium?
get_cookie(name) retrieves a specific cookie by its name.
What is get_cookies()?
get_cookies() returns all cookies stored for the current website.
How do I read a specific cookie?
driver.get_cookie("session_id")
How do I read all cookies?
driver.get_cookies()
What happens if the requested cookie does not exist?
get_cookie() returns None if no cookie with the specified name is found.
Key Takeaways
Reading cookies helps inspect browser session data during automation.
Use
get_cookie(name)to retrieve a specific cookie.Use
get_cookies()to retrieve all cookies for the current domain.Cookies store authentication data, user preferences, and session information.
Open the website before attempting to read cookies.
Always verify that a cookie exists before accessing its properties.
Reading cookies is useful for login validation, session management, and debugging.
Reading Cookies is an important Selenium browser session management concept and a frequently asked interview topic.
