Selenium
Browser automation library for Python that drives real browsers for testing and scraping.
Official Documentation ↗selenium 4.20+ / Python 3.9+ / 对应浏览器驱动. Check the official docs for the latest API surface.Installation
pip install seleniumMethods
No methods match this filter.
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:
NoSuchElementException(exception(WebDriverWait 默认忽略的异常类型)) — official text: “ignored_exceptions - iterable structure of exception classes ignored during calls. By default, it contains NoSuchElementException only.” (source: https://www.selenium.dev/selenium/docs/api/py/webdriver_support/selenium.webdriver.support.wait.html, fetched 2026-11-08T19:58:23+08:00)
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/.
webdriver.Chrome→ signature from https://www.selenium.dev/selenium/docs/api/py/webdriver_chrome/selenium.webdriver.chrome.webdriver.html; official text: “class selenium.webdriver.chrome.webdriver. WebDriver ( options : Options | None = None , service : Service | None = None , keep_alive : bool = True ) [source] Controls the ChromeDriver and allows you to drive the browser”WebDriverWait / find_element→ signature from https://www.selenium.dev/selenium/docs/api/py/webdriver_support/selenium.webdriver.support.wait.html; official text: “class selenium.webdriver.support.wait. WebDriverWait ( driver , timeout , poll_frequency=0.5 , ignored_exceptions=None ) [source] Constructor, takes a WebDriver instance and timeout in seconds.”
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.
Report a problem with this API docOpens a pre-filled issue on GitHub.