Bird
0
0

You want to verify if a cookie named auth exists and has value yes. Which code snippet correctly does this?

hard📝 Application Q8 of 15
Selenium Python - Advanced Patterns
You want to verify if a cookie named auth exists and has value yes. Which code snippet correctly does this?
Aif driver.get_cookies()['auth'] == 'yes': print('Valid')
Bcookie = driver.get_cookie('auth') if cookie and cookie['value'] == 'yes': print('Valid')
Ccookie = driver.get_cookie('auth') if cookie.value == 'yes': print('Valid')
Dif 'auth' in driver.get_cookies() and driver.get_cookies()['auth'] == 'yes': print('Valid')
Step-by-Step Solution
Solution:
  1. Step 1: Understand get_cookie return type

    The get_cookie('auth') returns a dictionary or None if cookie not found.
  2. Step 2: Check cookie existence and value

    cookie = driver.get_cookie('auth') if cookie and cookie['value'] == 'yes': print('Valid') correctly checks if cookie exists and then compares its 'value' key.
  3. Final Answer:

    cookie = driver.get_cookie('auth')\nif cookie and cookie['value'] == 'yes':\n print('Valid') -> Option B
  4. Quick Check:

    Use get_cookie() and check 'value' key safely [OK]
Quick Trick: Check cookie dict exists before accessing 'value' [OK]
Common Mistakes:
  • Assuming get_cookies() returns dict by name
  • Accessing cookie.value instead of cookie['value']
  • Not checking if cookie is None

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Selenium Python Quizzes