About this lesson
Selenium Python cookies: add_cookie, get_cookie and delete_cookie step by step
Selenium Python AssignmentsAdvancedPage Data
How it works
Cookies are small named values the browser keeps for a site. Selenium reaches them through three driver methods: add_cookie stores one, get_cookie reads one by name, and delete_cookie removes one. They work on the site that is currently open, so you must load a page from that site first.
The cookie goes in as a Python dict. It needs at least a name and a value key. Leave one out and you get InvalidArgumentException with a message like missing 'name'. What comes back from get_cookie is also a dict, with more keys such as domain, path, secure and sameSite. You pick the value with a dict key, not an attribute. If the name is not there, the call returns None instead of raising, so a missing cookie only shows up when you use the result.
That None is how you prove a delete worked. Comparing with is None gives a Python boolean. Printing it needs a string: adding a bool to a string raises TypeError with the message can only concatenate str (not "bool") to str, so wrap it with str. Setting a domain that does not match the open page raises InvalidCookieDomainException.
About the assignment
Round-trip one cookie: store it, read it back, remove it, and then confirm it is gone. Print two lines only, and leave the full cookie list alone.
What you will do
- With the Text Fields page already open, add a cookie with the name and the value given in the task.
- Read the cookie back by name and print its value. The value is one key of the returned dict.
- Delete the cookie by name.
- Read it once more, test whether the answer is
None, convert the result withstr, and print the line.
Common mistakes
Adding the cookie before opening a page
A cookie belongs to the open site, so load the page first, then add.
Indexing a missing cookie
After the delete,
get_cookiereturnsNone. Taking["value"]from it raisesTypeError: 'NoneType' object is not subscriptable. Check forNonefirst.Passing the name only
A dict with just a name fails with
InvalidArgumentException: invalid argument: missing 'value'. Both keys are required.
In a real test suite
Teams use cookies to skip the login screen. A fixture logs in once through the API, then adds the session cookie to the browser before each test, which saves many seconds per test. Cookies are also cleared between tests so no state leaks.
The next lesson reads another piece of page data: the raw HTML source.
Related lessons
- Search the Page SourceAlso reads data about the page instead of a visible element, using the HTML source.
- Visit Two Pages with driver.get()Shows
get, which loads the page a cookie needs before it can be added. - Set the Window SizeAnother place where the driver returns a dict, here with the window size.
To try one of these calls on any page without a task, open the Online Selenium Python Compiler.
Starter code
The editor above opens with this code. Fill in the TODO lines, then run and submit it.
from selenium import webdriver
driver = webdriver.Chrome()
try:
driver.get("https://testkru.com/Elements/TextFields")
# Add testCookie=SeleniumPython, print it, delete it
finally:
driver.quit()