Python selenium 库

Drivers, waits, selectors, and the difference between implicit and explicit waits.

Intermediate
Key PointsNever use `time.sleep` to wait for elements — use explicit `WebDriverWait` with a condition.

Setup

Selenium 4 manages drivers automatically — no manual chromedriver download. Headless mode is what you want on a server.

Bash
pip install selenium
Python
from selenium import webdriver
from selenium.webdriver.chrome.options import Options

opts = Options()
opts.add_argument("--headless=new")
opts.add_argument("--no-sandbox")
opts.add_argument("--window-size=1440,900")

driver = webdriver.Chrome(options=opts)
driver.get("https://example.com")
print(driver.title)
driver.quit()
Output
Example Domain

Explicit Waits

Pages render asynchronously. Implicit waits only cover element lookup; conditions like visible/clickable need explicit waits.

Python
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, timeout=10)
btn = wait.until(EC.element_to_be_clickable((By.CSS_SELECTOR, "button.submit")))
btn.click()

rows = wait.until(EC.presence_of_all_elements_located((By.CSS_SELECTOR, "table tbody tr")))
print(len(rows))
Output
12

Selectors and Input

StrategyExampleNote
By.ID"login"Fastest, use when available
By.CSS_SELECTOR"form input[name=user]"Most flexible
By.XPATH"//button[text()='提交']"Text matching
By.CLASS_NAME"item"Single class only
Python
driver.find_element(By.ID, "user").send_keys("alice")
driver.find_element(By.ID, "pwd").send_keys("secret")
driver.find_element(By.CSS_SELECTOR, "button[type=submit]").click()
Read robots.txt and the terms first. Aggressive automation puts real load on the target and may violate its ToS.

Friends & Partners