DEV Community

Anna lilith
Anna lilith

Posted on

Python Selenium Automation: From Zero to Production in 2026

Python Selenium Automation: From Zero to Production in 2026

Last updated: July 2026

Selenium is the gold standard for browser automation. This guide takes you from beginner to production-ready, with real-world examples that work.

Why Selenium in 2026?

Despite newer tools like Playwright, Selenium remains the most widely-used browser automation framework because:

  • Massive community — Thousands of tutorials, plugins, and solutions
  • Cross-browser — Works with Chrome, Firefox, Safari, Edge
  • Language support — Python, Java, C#, JavaScript, Ruby
  • Grid support — Run tests in parallel across multiple machines
  • Enterprise adoption — Most companies use Selenium for QA automation

Setting Up Selenium in Python

# Install
# pip install selenium webdriver-manager

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
from webdriver_manager.chrome import ChromeDriverManager
from selenium.webdriver.chrome.service import Service

# Auto-manage ChromeDriver
service = Service(ChromeDriverManager().install())
driver = webdriver.Chrome(service=service)

# Navigate to a page
driver.get("https://example.com")

# Find an element
title = driver.find_element(By.TAG_NAME, "h1").text
print(f"Page title: {title}")

# Clean up
driver.quit()
Enter fullscreen mode Exit fullscreen mode

Common Automation Patterns

1. Form Filling

def fill_form(driver, data):
    """Fill out a form with the given data."""
    for field, value in data.items():
        element = driver.find_element(By.NAME, field)
        element.clear()
        element.send_keys(value)

    # Submit
    driver.find_element(By.CSS_SELECTOR, "button[type='submit']").click()
Enter fullscreen mode Exit fullscreen mode

2. Waiting for Elements

def wait_for_element(driver, selector, timeout=10):
    """Wait for an element to be present."""
    return WebDriverWait(driver, timeout).until(
        EC.presence_of_element_located((By.CSS_SELECTOR, selector))
    )
Enter fullscreen mode Exit fullscreen mode

3. Handling Dynamic Content

def wait_for_ajax(driver, timeout=10):
    """Wait for AJAX requests to complete."""
    WebDriverWait(driver, timeout).until(
        lambda d: d.execute_script("return jQuery.active == 0")
    )
Enter fullscreen mode Exit fullscreen mode

4. Screenshot Capture

def take_screenshot(driver, filename):
    """Capture a screenshot of the current page."""
    driver.save_screenshot(filename)
    print(f"Screenshot saved: {filename}")
Enter fullscreen mode Exit fullscreen mode

Production Best Practices

1. Use Page Object Model

class LoginPage:
    def __init__(self, driver):
        self.driver = driver
        self.username_field = (By.ID, "username")
        self.password_field = (By.ID, "password")
        self.login_button = (By.ID, "login-btn")

    def login(self, username, password):
        self.driver.find_element(*self.username_field).send_keys(username)
        self.driver.find_element(*self.password_field).send_keys(password)
        self.driver.find_element(*self.login_button).click()
Enter fullscreen mode Exit fullscreen mode

2. Handle Exceptions

from selenium.common.exceptions import TimeoutException, NoSuchElementException

def safe_find(driver, selector, timeout=5):
    """Safely find an element with timeout."""
    try:
        return WebDriverWait(driver, timeout).until(
            EC.presence_of_element_located((By.CSS_SELECTOR, selector))
        )
    except (TimeoutException, NoSuchElementException):
        return None
Enter fullscreen mode Exit fullscreen mode

3. Use Headless Mode for CI/CD

def create_headless_driver():
    """Create a headless Chrome driver for CI/CD."""
    options = webdriver.ChromeOptions()
    options.add_argument("--headless")
    options.add_argument("--no-sandbox")
    options.add_argument("--disable-dev-shm-usage")
    return webdriver.Chrome(options=options)
Enter fullscreen mode Exit fullscreen mode

4. Rotate User Agents

import random

USER_AGENTS = [
    "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36...",
    "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36...",
    "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36...",
]

def create_driver_with_ua():
    """Create a driver with a random user agent."""
    options = webdriver.ChromeOptions()
    options.add_argument(f"--user-agent={random.choice(USER_AGENTS)}")
    return webdriver.Chrome(options=options)
Enter fullscreen mode Exit fullscreen mode

Real-World Example: Web Scraping

from selenium import webdriver
from selenium.webdriver.common.by import By
import csv

def scrape_products(url):
    """Scrape product listings from a website."""
    driver = create_driver_with_ua()
    driver.get(url)

    products = []
    items = driver.find_elements(By.CSS_SELECTOR, ".product-item")

    for item in items:
        try:
            name = item.find_element(By.CSS_SELECTOR, ".product-name").text
            price = item.find_element(By.CSS_SELECTOR, ".product-price").text
            products.append({"name": name, "price": price})
        except:
            continue

    driver.quit()
    return products

# Save to CSV
products = scrape_products("https://example.com/products")
with open("products.csv", "w", newline="") as f:
    writer = csv.DictWriter(f, fieldnames=["name", "price"])
    writer.writeheader()
    writer.writerows(products)
Enter fullscreen mode Exit fullscreen mode

Get the Production-Ready Version

We have a complete Selenium automation toolkit at our store.

What's included:

  • Pre-built page objects for common sites
  • Headless mode configuration
  • Screenshot capture utilities
  • Error handling and retry logic
  • Anti-detection measures

Browse the collection →

Top comments (0)