Selenium
Python bindings for Selenium WebDriver — browser automation for Chrome, Firefox, Edge, Safari. Current version is 4.41.0 (Feb 2026). Requires Python >=3.10. Selenium Manager (built-in since 4.6) automatically downloads and manages browser drivers — manual chromedriver management is no longer needed. All find_element_by_*() shorthand methods were removed in Selenium 4.
Warnings
- breaking All find_element_by_*() shorthand methods removed in Selenium 4. driver.find_element_by_id(), find_element_by_xpath(), find_element_by_css_selector(), find_elements_by_class_name() etc. all raise AttributeError. Still the most common LLM-generated Selenium footgun.
- breaking Python 3.9 dropped in Selenium 4.33+. Now requires Python >=3.10.
- breaking desired_capabilities parameter deprecated and removed from WebDriver constructors. Passing desired_capabilities= raises TypeError in modern Selenium 4.
- breaking options.add_argument('--headless') (old Chrome headless) deprecated. Chrome switched to a new headless implementation — use --headless=new instead.
- gotcha Selenium Manager automatically downloads matching browser drivers since 4.6. Manually downloading chromedriver and putting it on PATH is no longer necessary — but if you have a stale chromedriver on PATH, it may conflict with Selenium Manager.
- gotcha Not using WebDriverWait causes flaky tests. Immediately calling find_element() after page navigation often fails because the element hasn't loaded yet.
- gotcha driver.quit() must be called to close the browser process. Using driver.close() only closes the current window but leaves the browser process running. Always use try/finally or context manager.
Install
-
pip install selenium
Imports
- webdriver
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 # Selenium Manager handles chromedriver automatically driver = webdriver.Chrome() driver.get('https://example.com') # Correct element finding (Selenium 4) element = driver.find_element(By.ID, 'username') elements = driver.find_elements(By.CSS_SELECTOR, '.item') driver.quit()
Quickstart
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 selenium.webdriver.chrome.options import Options
# Selenium Manager downloads chromedriver automatically
options = Options()
options.add_argument('--headless=new') # new headless mode
options.add_argument('--no-sandbox')
options.add_argument('--disable-dev-shm-usage')
driver = webdriver.Chrome(options=options)
try:
driver.get('https://example.com')
# Wait for element before interacting
wait = WebDriverWait(driver, timeout=10)
button = wait.until(
EC.element_to_be_clickable((By.ID, 'submit-btn'))
)
button.click()
# Find elements
items = driver.find_elements(By.CSS_SELECTOR, '.product-item')
print(f'Found {len(items)} items')
finally:
driver.quit()