Skip to content

Selenium

The original browser automation tool — drives a real browser through the WebDriver protocol.

Web & HTTPWeb AutomationPython

What it is

Selenium is a powerful Python library for automating web browsers. It allows you to programmatically interact with web pages, simulate user actions, and perform browser testing across multiple browsers.

Selenium allows you to control browsers using WebDriver. You can navigate pages, click buttons, fill forms, extract text, take screenshots, handle alerts, and perform automated testing. It supports Chrome, Firefox, Edge, Safari, and other major browsers.

Watch for
Explicit waits are essential — implicit waits are the main source of flaky Selenium tests
Licence
Apache 2.0

When to use it

The question documentation cannot answer for you — because it cannot recommend something else.

Reach for it when

  • Testing against browsers and versions Playwright does not cover
  • An existing test suite already built on it
  • Legacy enterprise environments where WebDriver is the sanctioned standard

Look elsewhere when

  • Starting fresh — Playwright is faster, less flaky and has better waiting semantics

Installation

pip install selenium

Getting started

The smallest useful thing you can do with it, and what each part means.

Opening a webpage
from selenium import webdriver

driver = webdriver.Chrome()
driver.get('https://example.com')
print(driver.title)
driver.quit()
Launches Chrome, navigates to the URL, prints the page title, and closes the browser.
Finding elements by ID or Name
from selenium import webdriver
from selenium.webdriver.common.by import By

driver = webdriver.Chrome()
driver.get('https://example.com')
element = driver.find_element(By.ID, 'username')
element.send_keys('myuser')
driver.quit()
Locates an input field using its ID and types text into it.

Advanced usage

Where the library earns its place over a simpler alternative.

Clicking a button
from selenium.webdriver.common.by import By
button = driver.find_element(By.XPATH, '//button[text()="Submit"]')
button.click()
Finds a button by XPath and simulates a click.
Waiting for elements
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC

wait = WebDriverWait(driver, 10)
element = wait.until(EC.presence_of_element_located((By.ID, 'result')))
Waits up to 10 seconds for an element to appear before proceeding.
Handling alerts
alert = driver.switch_to.alert
alert.accept()
Switches to a JavaScript alert and accepts it.
Taking screenshots
driver.save_screenshot('screenshot.png')
Saves a screenshot of the current browser window.
Headless mode
from selenium.webdriver.chrome.options import Options
options = Options()
options.headless = True
driver = webdriver.Chrome(options=options)
Runs Chrome in headless mode for faster automation without opening a visible window.

Errors and fixes

The failures you are most likely to hit, and what actually resolves them.

NoSuchElementException
Verify the selector used to locate the element and ensure it exists on the page.
WebDriverException: Message: 'chromedriver' executable needs to be in PATH
Download the correct WebDriver for your browser and ensure it is in your system PATH.
TimeoutException
Increase the wait time or check that the expected condition is correct.

Best practices

  • Use explicit waits (`WebDriverWait`) instead of arbitrary sleep times for stability.
  • Close or quit browser instances to free resources.
  • Use headless mode for automation scripts that don't require UI interaction.
  • Leverage browser-specific options and capabilities for optimized performance.
  • Keep WebDriver versions compatible with the browser version installed.

Alternatives

Comparable options, and the reason you would pick one over the other.

Background

Why it exists, and what it was reacting to.

Selenium was originally developed in 2004 by Jason Huggins at ThoughtWorks as an internal tool for automated testing of web applications. Over time, it evolved into a widely-used framework for browser automation, supporting multiple programming languages and browser drivers. Selenium WebDriver became the standard API for reliable cross-browser automation.