API Reference

Selenium

Browser automation library for Python that drives real browsers for testing and scraping.

Official Documentation ↗
CompatibilityThis documentation covers version selenium 4.20+ / Python 3.9+ / 对应浏览器驱动. Check the official docs for the latest API surface.

Installation

pippip install selenium

Methods

Overview

Selenium automates real browsers, so it can test and scrape pages that depend on JavaScript. It drives Chrome, Firefox and others through the WebDriver protocol.

Core Methods

Start a driver, navigate with get, locate elements with find_element, and interact with click and send_keys. Waits synchronize with dynamic content better than fixed sleeps.

Usage Notes

Use explicit waits, run headless for automation, and scope selectors to stable attributes. Close or quit the driver in a finally block to release the browser process.

Common Pitfalls

Fixed sleeps make tests slow and flaky. Forgetting driver.quit leaks processes. Headless containers without proper memory settings crash the browser. Anti-bot defenses may block automated traffic.

Errors

The official API reference declares the following error types:

API reference evidence

Fetched from official sources on 2026-11-08T19:58:23+08:00 via WebFetch https://www.selenium.dev/selenium/docs/api/py/webdriver_chrome/selenium.webdriver.chrome.webdriver.html;WebFetch https://www.selenium.dev/selenium/docs/api/py/webdriver_support/selenium.webdriver.support.wait.html;WebFetch https://www.selenium.dev/documentation/webdriver/browsers/chrome/.

FAQ

How do I avoid stale element errors?
Re-locate elements after the page changes and prefer waits over fixed sleeps. Stale references point to DOM nodes replaced by navigation or re-render.
Why does the driver fail to start?
The browser and driver versions must be compatible. Selenium Manager can fetch a matching driver, or you pin the driver version to the installed browser.
How do I run in Docker or CI?
Use headless mode, add the no-sandbox flag where required, and ensure the container has the browser and shared memory sized adequately, or Chrome may crash.