Cookies in selenium python

  • Cookie is a piece of information from website and saved by your web browser
  • Cookies are a way of remembering users and their interaction with the site by storing information in the cookie file as key value pairs.
  • It stores the login information like user name / email and password
  • HTTP cookie is also known as a web cookie, a browser cookie or Internet cookie.
  • One website cannot access the cookies set by other websites

Handling Cookie in Selenium Python

  • Each cookie is associated with a name, value, domain, path, termination, and status of whether it is safe or not. We validate the cookie using selenium, selenium can parse all the cookies
  • When testing a web application using the Selenium, you may need to create, update or delete a cookie to check the behavior of the website with and without cookies.
  • For example, when you automate e-commerce sites, you will set cookies for logins, products added to carts using cookies and the product you visited in or searched in past days

Get Cookies in python

get_cookies() used to return the list of all cookies stored in the web browser.


# Go to the correct domain
driver.get("https://www.example.com")
# And now output all the available cookies for the current URL
driver.get_cookies()		

Get Cookie by name in python selenium

get_cookie() used to return the specific cookie according to its name.


driver.get_cookie('foo')			

Add Cookie in python selenium

add_cookie() method is used to create and add the cookie.


# Go to the correct domain
driver.get("https://chercher.tech")
# Now set the cookie. This one's valid for the entire domain
cookie = {‘name’ : ‘foo’, ‘value’ : ‘bar’}
driver.add_cookie(cookie)		

Delete Cookie in python

Using delete_cookie() to delete a cookie according to its name.


driver.delete_cookie(“foo?)			

Delete All Cookie in python selenium

delete_all_cookies() is used to delete all cookies.


								driver.delete_all_cookies()			
								

About Author

Article is written by Pavan (a) KarthiQ. Well, I am serving notice period in an MNC, Bangalore. I thought to enrich every person knowledge a little, I always have a feeling, when we teach something, we will learn more than what you know. Knowledge is the only thing that doubles when you spend it.

I have also created the reporter for Protractor Jasmine. Use for your projects without any hesitation

You can also share knowledge by Adding a topic here


Comment / Suggestion Section
Point our Mistakes and Post Your Suggestions