VOOZH about

URL: https://www.geeksforgeeks.org/python/adding-and-deleting-cookies-in-selenium-python/

⇱ Adding and Deleting Cookies in Selenium Python - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

Adding and Deleting Cookies in Selenium Python

Last Updated : 21 Aug, 2025

Selenium’s Python Module is built to perform automated testing with Python. Selenium Python bindings provide a simple API to write functional/acceptance tests using Selenium WebDriver.

Selenium WebDriver provides several methods to control the browser session, such as adding cookies, navigating back, switching tabs, and more. Managing cookies is often an important part of testing, especially when simulating scenarios like authentication where cookies may need to be manually added or removed. Selenium’s Python WebDriver offers methods to add, retrieve, and delete cookies, enabling testers to handle various practical use cases efficiently.

Cookie Methods in Selenium

Selenium WebDriver provides various methods to manage cookies.

1. add_cookie driver method

add_cookie method is used to add a cookie to your current session. This cookie can be used by website itself or by you. 

Syntax -

add_cookie(cookie_dict)

Example - Now one can use add_cookie method as a driver method as below -

driver.add_cookie({‘name’ : ‘foo’, ‘value’ : ‘bar’})

Read More - add_cookie driver method.

2. get_cookie driver method

get_cookie method is used to get a cookie with a specified name. It returns the cookie if found, None if not. 

Syntax -

driver.get_cookie(name)

Example - Now one can use get_cookie method as a driver method as below -

driver.get("https://www.geeksforgeeks.org///")
driver.get_cookie("foo")

Read More -  get_cookie driver method.

3. delete_cookie driver method

delete_cookie method is used to delete a cookie with a specified value. 

Syntax -

driver.delete_cookie(name)

Example - Now one can use delete_cookie method as a driver method as below -

driver.get("https://www.geeksforgeeks.org///")
driver.delete_cookie("foo")

Read More -  delete_cookie driver method.

4. get_cookies driver method

get_cookies method is used to get all cookies in current session. It returns a set of dictionaries, corresponding to cookies visible in the current session. Syntax -

driver.get_cookies()

Example - Now one can use get_cookies method as a driver method as below -

driver.get("https://www.geeksforgeeks.org///")
driver.get_cookies()

Read More - get_cookies driver method.

Example 1: Adding and Verifying a Cookie

We will demonstrate these methods by testing cookie management on https://www.geeksforgeeks.org//, We’ll add a cookie, retrieve it, verify its presence, and delete it.

Output:

👁 output-of-Selenium-handling-Cookies-with-python
Output of Adding and Verifying a Cookie

Example 2: Managing All Cookies and Deleting One

We will demonstrate List all cookies, add a custom cookie, delete it, and confirm its removed or not.

Output:

👁 Screenshot-2025-06-02-135955
Output of Managing All Cookies and Deleting One

Managing cookies in Selenium Python is straightforward with methods like add_cookie(), get_cookie(), delete_cookie(), and get_cookies(). These will let you simulate user sessions, test authentication, and verify personalization, making your tests faster and reliable.

Comment
Article Tags: