Why Selenium is the standard for web automation in Selenium Python - Automation Benefits in Action
from selenium import webdriver from selenium.webdriver.chrome.service import Service from selenium.webdriver.common.by import By from selenium.webdriver.support.ui import WebDriverWait from selenium.webdriver.support import expected_conditions as EC # Setup ChromeDriver service service = Service() # Initialize WebDriver driver = webdriver.Chrome(service=service) try: # Open the Selenium official website driver.get('https://www.selenium.dev/') # Wait until the page title contains 'Selenium' WebDriverWait(driver, 10).until(EC.title_contains('Selenium')) # Get the page title title = driver.title # Assert the title contains 'Selenium' assert 'Selenium' in title, f"Title does not contain 'Selenium': {title}" finally: # Close the browser driver.quit()
This script uses Selenium WebDriver with Python to open the Chrome browser and navigate to the Selenium official website.
We use WebDriverWait with expected_conditions.title_contains to wait explicitly until the page title contains the word 'Selenium'. This is better than a fixed sleep because it waits only as long as needed.
We then get the page title and assert it contains 'Selenium'. If the assertion fails, it will raise an error with a clear message.
The try-finally block ensures the browser closes even if an error occurs, which is a good practice to avoid leaving browser windows open.
This simple test shows why Selenium is a standard: it can control browsers, wait for conditions, and verify web page content reliably.
Now add data-driven testing to open three different URLs and verify their titles contain expected words