Bird
Raised Fist0
Selenium Pythontesting~20 mins

Cookie management in Selenium Python - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Cookie Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate
2:00remaining
What is the output of this Selenium cookie retrieval code?
Given the following Selenium Python code snippet, what will be printed?
Selenium Python
from selenium import webdriver
from selenium.webdriver.chrome.service import Service
from selenium.webdriver.chrome.options import Options

options = Options()
options.add_argument('--headless')
service = Service()
driver = webdriver.Chrome(service=service, options=options)
driver.get('https://example.com')
driver.add_cookie({'name': 'testcookie', 'value': '12345'})
cookie = driver.get_cookie('testcookie')
print(cookie['value'])
driver.quit()
A12345
BNone
CKeyError
Dselenium.common.exceptions.InvalidCookieDomainException
Attempts:
2 left
💡 Hint
Remember that after adding a cookie, you can retrieve it by its name.
❓ assertion
intermediate
1:30remaining
Which assertion correctly verifies a cookie named 'session' exists?
You want to assert that a cookie named 'session' is present in the browser using Selenium Python. Which assertion is correct?
Aassert driver.get_cookie('session')['value'] == ''
Bassert 'session' in driver.get_cookies()
Cassert driver.get_cookie('session') == {}
Dassert driver.get_cookie('session') is not None
Attempts:
2 left
💡 Hint
get_cookie returns None if cookie not found.
🔧 Debug
advanced
2:30remaining
Why does this Selenium code raise an InvalidCookieDomainException?
Consider this code snippet: from selenium import webdriver driver = webdriver.Chrome() driver.add_cookie({'name': 'user', 'value': 'abc'}) driver.get('https://example.com') Why does it raise selenium.common.exceptions.InvalidCookieDomainException?
ABecause the driver instance is not initialized properly.
BBecause the cookie dictionary is missing the 'domain' key.
CBecause add_cookie was called before navigating to any page, so no domain is set.
DBecause the cookie value 'abc' is invalid.
Attempts:
2 left
💡 Hint
Cookies must be added only after loading a page to set the domain.
❓ framework
advanced
3:00remaining
Which Selenium Python test framework setup correctly clears cookies before each test?
You want to clear all cookies before each test in pytest using Selenium WebDriver. Which setup is correct?
A
import pytest

@pytest.fixture(autouse=True)
def clear_cookies(driver):
    driver.delete_all_cookies()
B
import pytest

def clear_cookies(driver):
    driver.delete_all_cookies()

@pytest.fixture(autouse=True)
def setup():
    clear_cookies()
C
import pytest

@pytest.fixture(scope='session', autouse=True)
def clear_cookies(driver):
    driver.delete_all_cookies()
D
import pytest

@pytest.fixture(autouse=True)
def clear_cookies():
    driver.delete_all_cookies()
Attempts:
2 left
💡 Hint
Autouse fixtures run automatically before each test and can accept driver as argument.
🧠 Conceptual
expert
2:00remaining
What is the main reason to manage cookies explicitly in Selenium tests?
Why do testers often add, delete, or modify cookies explicitly in Selenium automated tests?
ATo speed up page loading by caching cookies locally.
BTo simulate user sessions and test authentication flows without manual login.
CTo bypass browser security restrictions on cross-site scripting.
DTo automatically generate test reports based on cookie data.
Attempts:
2 left
💡 Hint
Think about how cookies relate to user login and session state.

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

  1. Step 1: Understand what cookies do in web context

    Cookies store small data like session IDs or user preferences to remember users.
  2. Step 2: Relate cookie management in Selenium

    Selenium manages cookies to simulate user sessions and test behaviors depending on stored data.
  3. Final Answer:

    To store and manipulate small pieces of data like session info during tests -> Option B
  4. 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

  1. Step 1: Recall Selenium Python cookie syntax

    The method add_cookie expects a dictionary with keys 'name' and 'value'.
  2. Step 2: Match the correct syntax

    driver.add_cookie({'name': 'user', 'value': 'admin'}) correctly uses driver.add_cookie({'name': 'user', 'value': 'admin'}).
  3. Final Answer:

    driver.add_cookie({'name': 'user', 'value': 'admin'}) -> Option A
  4. Quick Check:

    add_cookie needs dict with 'name' and 'value' keys = A [OK]
Hint: Use a dict with 'name' and 'value' keys for add_cookie [OK]
Common Mistakes:
  • Using camelCase method names like addCookie
  • Passing cookie name and value as separate arguments
  • Passing a dict without 'name' and 'value' keys
3. What will be the output of the following code snippet?
driver.add_cookie({'name': 'session', 'value': 'xyz123'})
cookie = driver.get_cookie('session')
print(cookie['value'])
medium
A. {'name': 'session', 'value': 'xyz123'}
B. session
C. xyz123
D. null

Solution

  1. Step 1: Add cookie with name 'session' and value 'xyz123'

    The cookie is stored with key 'session' and value 'xyz123'.
  2. Step 2: Retrieve cookie and print its 'value'

    Calling get_cookie('session') returns the cookie dict; printing cookie['value'] outputs 'xyz123'.
  3. Final Answer:

    xyz123 -> Option C
  4. 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

  1. Step 1: Check Selenium Python method for deleting a cookie

    The method delete_cookie takes the cookie name as a string argument.
  2. Step 2: Verify the code usage

    Using driver.delete_cookie('user') correctly deletes the cookie named 'user'.
  3. Final Answer:

    No error; this is the correct way to delete a cookie by name -> Option D
  4. 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

  1. Step 1: Add cookie with correct dictionary format

    Use add_cookie({'name': 'theme', 'value': 'dark'}) to set the cookie properly.
  2. Step 2: Retrieve cookie and assert its 'value'

    Get the cookie dict with get_cookie('theme') and check cookie['value'] == 'dark'.
  3. Step 3: Delete the cookie by name

    Use delete_cookie('theme') to remove the cookie after test.
  4. Final Answer:

    Correct sequence with dict, assert on value, and delete by name -> Option A
  5. 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 unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard