跳至主要内容

Selenium Guide: Automating Browser Interaction

Since Selenium 4.6, developers no longer need to manually download ChromeDriver. Selenium automatically detects the local browser version and downloads the appropriate driver.


Getting Started​

1. Install the latest Selenium version​

pip install --upgrade selenium

2. Basic usage (no need to specify driver path)​

from selenium import webdriver

# Selenium 4.6+ automatically manages the driver
driver = webdriver.Chrome()
driver.get("https://www.google.com")
print(driver.title)
driver.quit()

The first run may take longer as it will download the appropriate driver.


Commonly Used Functions​

FeatureFunction & Description
Open a web pagedriver.get(url): open a specific URL
Get page titledriver.title: returns the current page title
Close browserdriver.quit(): closes the entire browser
Find single elementdriver.find_element(by=By.X, value="...")
Find multiple elementsdriver.find_elements(by=By.X, value="...")
Click an elementelement.click()
Enter textelement.send_keys("your text")
Clear text fieldelement.clear()
Get text from elementelement.text
Get/set attributeelement.get_attribute("href")
Scroll to elementdriver.execute_script("arguments[0].scrollIntoView();", element)

Common By locator options:

from selenium.webdriver.common.by import By
By.ID, By.CLASS_NAME, By.NAME, By.XPATH, By.CSS_SELECTOR, By.LINK_TEXT, By.PARTIAL_LINK_TEXT

How to Verify It Works​

Try this code to ensure everything is working:

from selenium import webdriver

driver = webdriver.Chrome()
driver.get("https://www.google.com")
print(driver.title)
driver.quit()

If it opens Google and prints the title without requiring a downloaded driver, then Selenium Manager is working


Additional Info​

How Selenium Auto-Handles ChromeDriver​

Since Selenium 4.6.0, the built-in Selenium Manager:

  • Detects your locally installed Chrome version
  • Downloads the corresponding ChromeDriver
  • Automatically sets the path; no need for manual config

Optional: Manual ChromeDriver Download​

If you prefer to manually manage the driver (e.g. offline use):

Example:

from selenium import webdriver

service = webdriver.ChromeService(executable_path="/path/to/chromedriver")
driver = webdriver.Chrome(service=service)