0
0
Selenium Pythontesting~20 mins

Cookie management in Selenium Python - Practice Problems & Coding Challenges

Choose your learning style9 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.