A cookie is a small piece of data stored by the browser to remember information about the user or session. In testing, cookies help verify user sessions and preferences.
Click to reveal answer
beginner
How do you add a cookie using Selenium in Python?
Use the method driver.add_cookie({'name': 'key', 'value': 'value'}) to add a cookie to the current browser session.
Click to reveal answer
beginner
How can you delete all cookies in Selenium Python?
Call driver.delete_all_cookies() to remove all cookies stored in the browser for the current domain.
Click to reveal answer
intermediate
Why is cookie management important in automated testing?
Managing cookies helps simulate user sessions, test login states, and verify security features like session expiration.
Click to reveal answer
beginner
What does driver.get_cookies() return in Selenium Python?
It returns a list of dictionaries, each representing a cookie with details like name, value, domain, and expiry.
Click to reveal answer
Which Selenium Python method adds a cookie to the browser?
Adriver.set_cookie()
Bdriver.create_cookie()
Cdriver.add_cookie()
Ddriver.insert_cookie()
✗ Incorrect
The correct method to add a cookie is driver.add_cookie() with a dictionary argument.
What does driver.delete_all_cookies() do?
ADeletes cookies from other domains
BDeletes cookies only for the current page
CDeletes cookies permanently from the browser
DDeletes all cookies for the current domain
✗ Incorrect
It deletes all cookies stored for the current domain in the browser session.
Which data type does driver.get_cookies() return?
AList of dictionaries
BDictionary
CList of strings
DString
✗ Incorrect
driver.get_cookies() returns a list where each item is a dictionary representing a cookie.
Why might you want to delete cookies during a test?
ATo speed up the browser
BTo simulate a new user session
CTo clear browser cache
DTo close the browser
✗ Incorrect
Deleting cookies helps simulate a fresh user session without previous data.
Which of these is NOT a typical cookie attribute in Selenium?
Acolor
Bvalue
Cname
Dexpiry
✗ Incorrect
Cookies do not have a 'color' attribute; common attributes include name, value, domain, path, and expiry.
Explain how to add, retrieve, and delete cookies in Selenium Python.
Think about the methods that start with 'add', 'get', and 'delete'.
You got /3 concepts.
Why is managing cookies important when automating web tests?
Consider what cookies store and how they affect user experience.
You got /3 concepts.
Practice
(1/5)
1. What is the primary purpose of managing cookies in Selenium tests?
easy
A. To install browser extensions automatically
B. To store and manipulate small pieces of data like session info during tests
C. To speed up the browser loading time
D. To change the browser's homepage
Solution
Step 1: Understand what cookies do in web context
Cookies store small data like session IDs or user preferences to remember users.
Step 2: Relate cookie management in Selenium
Selenium manages cookies to simulate user sessions and test behaviors depending on stored data.
Final Answer:
To store and manipulate small pieces of data like session info during tests -> Option B
Quick Check:
Cookies store session data = D [OK]
Hint: Cookies hold session data for tests, not browser settings [OK]
Common Mistakes:
Thinking cookies change browser settings
Confusing cookies with browser speed optimization
Assuming cookies install extensions
2. Which of the following is the correct way to add a cookie named user with value admin in Selenium Python?
easy
A. driver.add_cookie({'name': 'user', 'value': 'admin'})
B. driver.addCookie('user', 'admin')
C. driver.add_cookie('user', 'admin')
D. driver.addCookie({'user': 'admin'})
Solution
Step 1: Recall Selenium Python cookie syntax
The method add_cookie expects a dictionary with keys 'name' and 'value'.
Step 1: Add cookie with name 'session' and value 'xyz123'
The cookie is stored with key 'session' and value 'xyz123'.
Step 2: Retrieve cookie and print its 'value'
Calling get_cookie('session') returns the cookie dict; printing cookie['value'] outputs 'xyz123'.
Final Answer:
xyz123 -> Option C
Quick Check:
get_cookie('session')['value'] = 'xyz123' [OK]
Hint: get_cookie returns dict; access 'value' key for cookie value [OK]
Common Mistakes:
Printing cookie name instead of value
Expecting get_cookie to return just the value string
Confusing cookie dict with string
4. Identify the error in this Selenium Python code for deleting a cookie:
driver.delete_cookie('user')
medium
A. Method name should be deleteCookie instead of delete_cookie
B. delete_cookie requires a dictionary, not a string
C. Must call driver.delete_all_cookies() instead
D. No error; this is the correct way to delete a cookie by name
Solution
Step 1: Check Selenium Python method for deleting a cookie
The method delete_cookie takes the cookie name as a string argument.
Step 2: Verify the code usage
Using driver.delete_cookie('user') correctly deletes the cookie named 'user'.
Final Answer:
No error; this is the correct way to delete a cookie by name -> Option D
Quick Check:
delete_cookie('name') is correct syntax = C [OK]
Hint: delete_cookie takes cookie name string, not dict [OK]
Common Mistakes:
Using camelCase method names
Passing dict instead of string
Confusing delete_cookie with delete_all_cookies
5. You want to test a website's behavior when a cookie named theme is set to dark. Which sequence of Selenium Python commands correctly sets this cookie, verifies it, and then deletes it?
hard
A. driver.add_cookie({'name': 'theme', 'value': 'dark'})\ncookie = driver.get_cookie('theme')\nassert cookie['value'] == 'dark'\ndriver.delete_cookie('theme')
B. driver.add_cookie('theme', 'dark')\ncookie = driver.get_cookie('theme')\nassert cookie == 'dark'\ndriver.delete_cookie('theme')
C. driver.add_cookie({'theme': 'dark'})\ncookie = driver.get_cookie('theme')\nassert cookie['value'] == 'dark'\ndriver.delete_cookie('theme')
D. driver.add_cookie({'name': 'theme', 'value': 'dark'})\ncookie = driver.get_cookie('theme')\nassert cookie == 'dark'\ndriver.delete_cookie('theme')
Solution
Step 1: Add cookie with correct dictionary format
Use add_cookie({'name': 'theme', 'value': 'dark'}) to set the cookie properly.
Step 2: Retrieve cookie and assert its 'value'
Get the cookie dict with get_cookie('theme') and check cookie['value'] == 'dark'.
Step 3: Delete the cookie by name
Use delete_cookie('theme') to remove the cookie after test.
Final Answer:
Correct sequence with dict, assert on value, and delete by name -> Option A
Quick Check:
add_cookie dict + get_cookie['value'] + delete_cookie(name) = B [OK]
Hint: Add cookie with dict, assert value key, delete by name [OK]
Common Mistakes:
Passing cookie as separate args instead of dict
Asserting cookie object equals string
Using wrong keys in cookie dict
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.