Verify Google Search page title using headless Chrome browser
Preconditions (3)
✅ Expected Result: The page title contains 'Google' and the test completes without opening a visible browser window
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.
Now add data-driven testing to verify page titles for three different URLs in headless mode
from selenium import webdriver
options = webdriver.ChromeOptions()
options.add_argument('--headless')
driver = webdriver.Chrome(options=options)
driver.get('https://example.com')
print(driver.title)
driver.quit()from selenium import webdriver
options = webdriver.ChromeOptions()
driver = webdriver.Chrome(options=options)
options.add_argument('--headless')
driver.get('https://example.com')Pass the test and this pattern moves unknown