Skip to content

Automating UI Tests with Selenium & Python

When Selenium is the right tool

  • you must test real browser rendering
  • you need to click, type, and navigate
  • pages are dynamic

Minimal example

selenium_smoke.py
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()
selenium_smoke.py
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

  • Prefer explicit waits
  • Use stable selectors (id, data-testid)
  • Keep UI tests few (test pyramid)

If this helped you, consider buying me a coffee ☕

Buy me a coffee

Was this page helpful?

Let us know how we did