Open In App

Adding and Deleting Cookies in Selenium Python

Improve
Improve
Like Article
Like
Save
Share
Report

Selenium’s Python Module is built to perform automated testing with Python. Selenium Python bindings provides a simple API to write functional/acceptance tests using Selenium WebDriver. To open a webpage using Selenium Python, checkout – Navigating links using get method – Selenium Python. Just being able to go to places isn’t terribly useful. What we’d really like to do is to interact with the pages, or, more specifically, the HTML elements within a page. There are multiple strategies to find an element using Selenium, checkout – Locating Strategies. Selenium WebDriver offers various useful methods to control the session, or in other words, browser. For example, adding a cookie, pressing back button, navigating among tabs, etc.
Playing around with cookies is often essential a cookie might need to be added manually or removed manually to implement some stage of website such as authentication. Various method to play around with cookies in selenium python are –

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’})

To check individual implementation of add_cookie method in project visit – add_cookie driver method.

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")

To check individual implementation of get_cookie method in project visit – get_cookie driver method.

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")

To check individual implementation of delete_cookie method in project visit – delete_cookie driver method.

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()

To check individual implementation of get_cookies method in project visit – get_cookies driver method.

How to use cookies in Selenium Python ?

To demonstrate, cookies in Selenium Python. Let’ s visit https://www.geeksforgeeks.org/ and operate on driver object.

Program –




# import webdriver
from selenium import webdriver
  
# create webdriver object
driver = webdriver.Firefox()
  
# get geeksforgeeks.org
  
# add_cookie method driver
driver.add_cookie({"name" : "foo", "value" : "bar"})
  
# get browser cookie
driver.get_cookie("foo")
  
  
# get all cookies in scope of session
print(driver.get_cookies())
  
# delete browser cookie
driver.delete_cookie("foo")
  
# clear all cookies in scope of session
driver.delete_all_cookies()


Output –
Cookie with name foo and value bar is added as verified below –
add_cookie-driver-method-Selenium-Python
Terminal Output –
get_cookies-driver-method-Selenium-Python



Last Updated : 02 Jul, 2020
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads