Bird
Raised Fist0
Selenium Pythontesting~20 mins

Headless mode for CI in Selenium Python - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Headless CI 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 headless mode setup code?
Consider this Python Selenium code snippet that sets up a Chrome browser in headless mode for CI testing. What will be printed after running this code?
Selenium Python
from selenium import webdriver
from selenium.webdriver.chrome.options import Options

options = Options()
options.add_argument('--headless')
options.add_argument('--disable-gpu')

try:
    driver = webdriver.Chrome(options=options)
    driver.get('https://example.com')
    title = driver.title
    print(title)
    driver.quit()
except Exception as e:
    print(f'Error: {e}')
AError: Message: 'chromedriver' executable needs to be in PATH
BNo output (empty string)
CError: WebDriverException: unknown error: Chrome failed to start
DExample Domain
Attempts:
2 left
💡 Hint
Headless mode runs Chrome without opening a window but still loads the page and its title.
❓ assertion
intermediate
1:30remaining
Which assertion correctly verifies the page title in headless mode?
You want to assert that the page title is exactly 'Example Domain' after loading the page in headless mode. Which assertion statement is correct?
Selenium Python
from selenium import webdriver
from selenium.webdriver.chrome.options import Options

options = Options()
options.add_argument('--headless')
driver = webdriver.Chrome(options=options)
driver.get('https://example.com')
title = driver.title

# Which assertion below is correct?
Aassert 'Example' in title and 'Domain' not in title
Bassert title != 'Example Domain'
Cassert title == 'Example Domain'
Dassert title == 'example domain'
Attempts:
2 left
💡 Hint
The page title is case sensitive and exactly 'Example Domain'.
🔧 Debug
advanced
2:30remaining
Why does this headless Chrome test fail with a WebDriverException?
This Selenium Python code runs in CI but fails with the error: 'unknown error: Chrome failed to start'. What is the most likely cause?
Selenium Python
from selenium import webdriver
from selenium.webdriver.chrome.options import Options

options = Options()
options.add_argument('--headless')
# Missing this option causes failure in some CI environments
# options.add_argument('--no-sandbox')

driver = webdriver.Chrome(options=options)
driver.get('https://example.com')
print(driver.title)
driver.quit()
AThe URL 'https://example.com' is unreachable in headless mode
BMissing '--no-sandbox' argument causes Chrome to fail in restricted CI environments
CThe ChromeDriver version is incompatible with Chrome browser
DHeadless mode is not supported on Linux
Attempts:
2 left
💡 Hint
Some CI environments restrict sandboxing, requiring a special Chrome option.
❓ locator
advanced
2:00remaining
Which locator strategy is best for stable element selection in headless CI tests?
You want to select a button with text 'Submit' in a page tested in headless mode. Which locator is most reliable for CI tests?
Adriver.find_element('xpath', "//button[text()='Submit']")
Bdriver.find_element('id', 'submit')
Cdriver.find_element('css selector', 'button.submit-btn')
Ddriver.find_element('class name', 'btn')
Attempts:
2 left
💡 Hint
Text-based XPath is precise if the button text is unique and stable.
❓ framework
expert
3:00remaining
Which CI pipeline step correctly runs Selenium tests in headless mode with Chrome on Linux?
You configure a CI pipeline to run Selenium Python tests with headless Chrome on a Linux runner. Which step setup is correct?
AInstall Chrome and ChromeDriver, run tests with Chrome options '--headless', '--no-sandbox', '--disable-dev-shm-usage'
BRun tests without installing Chrome, rely on default browser, use '--headless' only
CRun tests with GUI enabled, no headless options, and use Xvfb for virtual display
DInstall Firefox and use Selenium with Firefox headless mode instead of Chrome
Attempts:
2 left
💡 Hint
Linux CI runners often require special Chrome options and explicit installation.

Practice

(1/5)
1. What is the main benefit of running Selenium tests in headless mode on a Continuous Integration (CI) server?
easy
A. Tests require manual interaction to proceed
B. Tests can only run on Windows operating systems
C. Tests run faster and use fewer resources because no browser window is displayed
D. Tests produce more detailed screenshots automatically

Solution

  1. Step 1: Understand headless mode purpose

    Headless mode runs the browser without opening a visible window, saving system resources.
  2. Step 2: Relate to CI environment needs

    CI servers often run tests automatically without user interaction, so headless mode speeds up tests and reduces resource use.
  3. Final Answer:

    Tests run faster and use fewer resources because no browser window is displayed -> Option C
  4. Quick Check:

    Headless mode = faster, resource-saving tests [OK]
Hint: Headless means no browser window, so tests run faster [OK]
Common Mistakes:
  • Thinking headless mode requires manual input
  • Believing headless mode only works on Windows
  • Assuming headless mode automatically creates screenshots
2. Which of the following is the correct way to enable headless mode in Selenium with Python for Chrome?
easy
A. options = webdriver.ChromeOptions() options.headless = True browser = webdriver.Chrome(options=options)
B. options = webdriver.ChromeOptions() options.add_argument('--headless') browser = webdriver.Chrome(options=options)
C. browser = webdriver.Chrome() browser.headless = True
D. browser = webdriver.Chrome() browser.set_headless(True)

Solution

  1. Step 1: Recall correct syntax for headless Chrome

    In Selenium Python, headless mode is enabled by adding the argument '--headless' to ChromeOptions.
  2. Step 2: Check each option for correctness

    options = webdriver.ChromeOptions() options.add_argument('--headless') browser = webdriver.Chrome(options=options) correctly uses options.add_argument('--headless') and passes options to Chrome driver.
  3. Final Answer:

    options = webdriver.ChromeOptions() options.add_argument('--headless') browser = webdriver.Chrome(options=options) -> Option B
  4. Quick Check:

    Use add_argument('--headless') to enable headless mode [OK]
Hint: Use add_argument('--headless') on options for headless Chrome [OK]
Common Mistakes:
  • Setting options.headless = True (not supported)
  • Trying to set headless directly on browser instance
  • Using non-existent set_headless method
3. What will be the output of the following Selenium Python code snippet when run on a CI server?
from selenium import webdriver
options = webdriver.FirefoxOptions()
options.add_argument('--headless')
driver = webdriver.Firefox(options=options)
driver.get('https://example.com')
title = driver.title
driver.quit()
print(title)
medium
A. Prints the page title 'Example Domain' without opening a browser window
B. Raises an error because Firefox does not support headless mode
C. Prints an empty string because headless mode disables page loading
D. Opens a visible browser window and prints the title

Solution

  1. Step 1: Understand Firefox headless support

    Firefox supports headless mode via options.add_argument('--headless'), so the browser runs without UI.
  2. Step 2: Analyze code behavior

    The code loads 'https://example.com', gets the page title 'Example Domain', then quits the driver and prints the title.
  3. Final Answer:

    Prints the page title 'Example Domain' without opening a browser window -> Option A
  4. Quick Check:

    Headless Firefox loads page and returns title correctly [OK]
Hint: Headless mode loads pages normally but hides browser window [OK]
Common Mistakes:
  • Assuming headless disables page loading
  • Thinking Firefox lacks headless support
  • Expecting visible browser window in headless mode
4. You wrote this Selenium Python code to run Chrome in headless mode on a CI server, but tests fail with a timeout error:
options = webdriver.ChromeOptions()
options.add_argument('--headless')
driver = webdriver.Chrome(options=options)
driver.get('https://example.com')
What is the most likely cause and fix?
medium
A. The URL 'https://example.com' is invalid; use 'http://example.com'
B. Headless mode is not supported on Chrome; remove '--headless' argument
C. You must call driver.maximize_window() before get() in headless mode
D. Missing '--no-sandbox' argument; add options.add_argument('--no-sandbox') for CI environments

Solution

  1. Step 1: Identify common CI environment issue

    On many CI servers, Chrome needs '--no-sandbox' to run properly in headless mode to avoid permission issues.
  2. Step 2: Apply fix to options

    Add options.add_argument('--no-sandbox') along with '--headless' to fix timeout errors.
  3. Final Answer:

    Missing '--no-sandbox' argument; add options.add_argument('--no-sandbox') for CI environments -> Option D
  4. Quick Check:

    Use '--no-sandbox' with headless Chrome on CI [OK]
Hint: Add '--no-sandbox' for headless Chrome on CI to avoid timeouts [OK]
Common Mistakes:
  • Removing headless argument thinking it's unsupported
  • Calling maximize_window() unnecessarily in headless
  • Changing URL without reason
5. You want to run Selenium tests in headless mode on a CI server using Chrome, but your tests intermittently fail because some elements are not interactable. Which combined approach best solves this problem?
hard
A. Enable headless mode with '--headless=new', add '--disable-gpu', and set a window size with options.add_argument('--window-size=1920,1080')
B. Use headless mode without window size and rely on default browser size
C. Disable headless mode and run tests with visible browser windows on CI
D. Add '--incognito' mode only without changing headless or window size

Solution

  1. Step 1: Understand headless mode limitations

    Headless mode can cause layout differences; setting window size helps elements render correctly.
  2. Step 2: Use modern headless flag and disable GPU

    Using '--headless=new' enables the latest headless mode in Chrome, and '--disable-gpu' avoids GPU issues in CI.
  3. Step 3: Combine all options for stable tests

    Adding '--window-size=1920,1080' ensures consistent viewport, preventing element interaction failures.
  4. Final Answer:

    Enable headless mode with '--headless=new', add '--disable-gpu', and set a window size with options.add_argument('--window-size=1920,1080') -> Option A
  5. Quick Check:

    Use new headless, disable GPU, and set window size for stable CI tests [OK]
Hint: Set window size and use new headless mode for stable CI tests [OK]
Common Mistakes:
  • Ignoring window size causing element not interactable errors
  • Using old headless mode without '--headless=new'
  • Disabling headless mode defeats CI automation purpose
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