Python Selenium
About Lesson

In this Python Selenium tutorial we are going to learn about Handling Cookies in Selenium, so cookies are small piece of data stored on the user’s computer by the web browser while browsing a website, usually cookies stores login information.

 

OK first of all we want to get all cookies, so you can use get_cookies() function for getting the all cookies from the browser in selenium, and It returns a ‘successful cookie data’ for current browsing context. If browser is no longer available it returns error.

 

 

 

If you run the code, you can see that we have two cookies.

Python Selenium - Handling Cookies
Python Selenium – Handling Cookies

 

 

 

Also you can add new cookie, for adding the cookies in selenium you can use add_cookie() method, add_cookie() only accepts a set of defined serializable JSON object. so in this code we are going to add a new cookie to the website, you need to give the cookie name and also cookie value.

 

 

 

Run the code and you can see that our cookie is added, we have the name of the cookie with the value of the cookie.

Python Selenium Add Cookie
Python Selenium Add Cookie

 

 

 

Also you can delete a cookie, for deleting you can use delete_cookie() method, and It deletes the cookie data matching with the provided cookie name.