0
0
Selenium Pythontesting~15 mins

Headless browser execution in Selenium Python - Build an Automation Script

Choose your learning style9 modes available
Verify Google Search page title using headless Chrome browser
Preconditions (3)
Step 1: Launch Chrome browser in headless mode
Step 2: Navigate to 'https://www.google.com'
Step 3: Wait until the page title is available
Step 4: Verify the page title contains the word 'Google'
Step 5: Close the browser
✅ Expected Result: The page title contains 'Google' and the test completes without opening a visible browser window
Automation Requirements - Selenium with Python
Assertions Needed:
Assert that the page title contains the word 'Google'
Best Practices:
Use explicit waits to wait for page title
Use ChromeOptions to enable headless mode
Properly close the browser after test
Use By and WebDriverWait from selenium.webdriver.support
Automated Solution
Selenium Python
from selenium import webdriver
from selenium.webdriver.chrome.service import Service
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC

# Setup Chrome options for headless mode
options = Options()
options.add_argument('--headless=new')  # Use new headless mode for Chrome 109+
options.add_argument('--disable-gpu')  # Disable GPU for stability

# Setup ChromeDriver service (adjust path if needed)
service = Service()

# Create WebDriver instance with options
driver = webdriver.Chrome(service=service, options=options)

try:
    # Navigate to Google
    driver.get('https://www.google.com')

    # Wait up to 10 seconds for title to contain 'Google'
    WebDriverWait(driver, 10).until(EC.title_contains('Google'))

    # Assert title contains 'Google'
    assert 'Google' in driver.title, f"Title does not contain 'Google': {driver.title}"

finally:
    # Close the browser
    driver.quit()

This script uses Selenium with Python to open Google in a headless Chrome browser.

We set ChromeOptions to enable headless mode so no browser window appears.

We use WebDriverWait with title_contains to wait until the page title includes 'Google'.

Then we assert the title contains 'Google' to verify the page loaded correctly.

Finally, we close the browser with driver.quit() to clean up.

This approach ensures the test runs without a visible browser and waits properly for the page to load.

Common Mistakes - 4 Pitfalls
Not using explicit waits and immediately asserting the title
{'mistake': 'Forgetting to add headless argument in ChromeOptions', 'why_bad': 'The browser will open visibly, defeating the purpose of headless testing.', 'correct_approach': "Add '--headless=new' argument to ChromeOptions to run Chrome in headless mode."}
Not closing the browser after test
{'mistake': "Using deprecated headless argument '--headless' instead of '--headless=new'", 'why_bad': "Newer Chrome versions recommend '--headless=new' for better stability and features.", 'correct_approach': "Use '--headless=new' for Chrome 109 and above."}
Bonus Challenge

Now add data-driven testing to verify page titles for three different URLs in headless mode

Show Hint