Challenge - 5 Problems
Cookie Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2: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()
Attempts:
2 left
💡 Hint
Remember that after adding a cookie, you can retrieve it by its name.
✗ Incorrect
The code adds a cookie named 'testcookie' with value '12345' to the current domain and then retrieves it. The print statement outputs the value '12345'.
❓ assertion
intermediate1: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?
Attempts:
2 left
💡 Hint
get_cookie returns None if cookie not found.
✗ Incorrect
driver.get_cookie('session') returns None if the cookie does not exist. So checking it is not None confirms presence.
🔧 Debug
advanced2: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?
Attempts:
2 left
💡 Hint
Cookies must be added only after loading a page to set the domain.
✗ Incorrect
Selenium requires the browser to be on a page before adding cookies, so the domain is known. Adding cookies before driver.get() causes this error.
❓ framework
advanced3: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?
Attempts:
2 left
💡 Hint
Autouse fixtures run automatically before each test and can accept driver as argument.
✗ Incorrect
Option A defines an autouse fixture that accepts the driver and deletes cookies before each test. Others either miss autouse or driver argument.
🧠 Conceptual
expert2: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?
Attempts:
2 left
💡 Hint
Think about how cookies relate to user login and session state.
✗ Incorrect
Managing cookies allows tests to simulate logged-in users by setting session cookies directly, avoiding repetitive manual login steps.