Automating UI Tests with Selenium & Python
When Selenium is the right tool
Section titled “When Selenium is the right tool”- you must test real browser rendering
- you need to click, type, and navigate
- pages are dynamic
Minimal example
Section titled “Minimal example”from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
def test_homepage_title():
driver = webdriver.Chrome()
try:
driver.get("https://example.com")
h1 = WebDriverWait(driver, 10).until(
EC.visibility_of_element_located((By.TAG_NAME, "h1"))
)
assert "Example" in h1.text
finally:
driver.quit()Best practices
Section titled “Best practices”- Prefer explicit waits
- Use stable selectors (id, data-testid)
- Keep UI tests few (test pyramid)
pch.coffeeTagline
pch.coffeeCtapch.feedbackHeading
pch.feedbackSubheading