Bird
0
0

Why does this Selenium code fail to run Chrome in headless mode?

medium📝 Debug Q7 of 15
Selenium Python - Cross-Browser Testing
Why does this Selenium code fail to run Chrome in headless mode?
from selenium import webdriver
options = webdriver.ChromeOptions()
options.add_argument('--headless')
driver = webdriver.Chrome()
driver.get('https://example.com')
AThe URL is invalid
BThe '--headless' argument is misspelled
CChrome does not support headless mode
DThe options object with headless argument is not passed to the Chrome driver
Step-by-Step Solution
Solution:
  1. Step 1: Check options usage

    Headless mode is enabled by adding '--headless' to options.
  2. Step 2: Passing options

    The options object must be passed to webdriver.Chrome(options=options).
  3. Step 3: Identify mistake

    Here, driver is created without options, so headless mode is not enabled.
  4. Final Answer:

    Options object not passed to Chrome driver -> Option D
  5. Quick Check:

    Pass options to Chrome() to enable headless [OK]
Quick Trick: Always pass options to Chrome() constructor [OK]
Common Mistakes:
  • Forgetting to pass options to driver
  • Misspelling '--headless' argument
  • Assuming headless mode is default

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Selenium Python Quizzes