Skip to content
CodeKruPro

Add, Read, and Delete a Cookie

74/80

Add, Read, and Delete a Cookie

Open the page first. Then add a cookie named testCookie with value SeleniumPython. After delete_cookie, get_cookie returns None.

Task:

  1. Add the cookie and print its value
  2. Delete it
  3. Print whether get_cookie is None after the delete

Do not print the whole cookie jar.

Print the lines below exactly.

Expected output:

Cookie value: SeleniumPython
Cookie deleted: True

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

  1. With the Text Fields page already open, add a cookie with the name and the value given in the task.
  2. Read the cookie back by name and print its value. The value is one key of the returned dict.
  3. Delete the cookie by name.
  4. Read it once more, test whether the answer is None, convert the result with str, 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_cookie returns None. Taking ["value"] from it raises TypeError: 'NoneType' object is not subscriptable. Check for None first.

  • 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.

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.

main.py
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()