Python selenium Library
Selenium is a powerful tool for automating web browser operations, widely used for web application testing, web data scraping, task automation and similar scenarios.
Selenium provides APIs for various programming languages for testing purposes. The current official API documentation covers C#, JavaScript, Java, Python and Ruby.
Selenium tutorial:https://www.runoob.com/selenium/
Install Selenium and WebDriver
Install Selenium
To get started with Selenium, you first need to install the selenium library and download the WebDriver for your browser.
Install Selenium with pip:
pip install selenium
After installation, you can check the selenium version with the following command:
pip show selenium
You can also check it from Python code:
import selenium
print(selenium.__version__)
Download WebDriver
Selenium needs a WebDriver to interact with the browser.
Different browsers need different WebDrivers; for example Chrome needs ChromeDriver. You need to download the WebDriver matching the browser you use and make sure it is in your system PATH.
- Chrome: ChromeDriver
- Firefox: GeckoDriver
- Edge: EdgeDriver
- Safari: SafariDriver
Choose a browser and initialize the WebDriver:
Example
from
selenium
import
webdriver
Use the Chrome browser
driver
=
webdriver.
Chrome
(
executable_path
=
'/path/to/chromedriver'
)
Or use the Firefox browser
driver = webdriver.Firefox(executable_path='/path/to/geckodriver')
Or use the Edge browser
driver = webdriver.Edge(executable_path='/path/to/msedgedriver')
Starting with Selenium 4, browser driver management has changed: Selenium 4 tries to detect the browser version installed on the system automatically and downloads the corresponding driver, which means users no longer need to download and set driver paths manually unless they need a specific driver version.
Example
from
selenium
import
webdriver
driver
=
webdriver.
Chrome
(
)
If you use another browser such as Firefox, you need to modify
accordingly. When working with the domestic network environment, automatic driver detection and download may require a different network environment, so it is recommended to download the driver manually and specify its path.
In Selenium 4 the driver path is no longer set directly on webdriver.Chrome; instead it is set by introducing a Service object. This avoids deprecation warnings and ensures the driver loads correctly. For example:
Example
from
selenium
import
webdriver
from
selenium.
webdriver
.
chrome
.
service
import
Service
as
ChromeService
service
=
ChromeService
(
executable_path
=
"PATH_TO_DRIVER"
)
options
=
webdriver.
ChromeOptions
(
)
driver
=
webdriver.
Chrome
(
service
=
service
,
options
=
options
)
The following code specifies the driver file path and gets the page title:
Example
from
selenium
import
webdriver
from
selenium.
webdriver
.
chrome
.
service
import
Service
as
ChromeService
Set the correct driver path
service
=
ChromeService
(
executable_path
=
"/RUNOOB/Downloads/chromedriver-mac-arm64/chromedriver"
)
options
=
webdriver.
ChromeOptions
(
)
driver
=
webdriver.
Chrome
(
service
=
service
,
options
=
options
)
Open a website
driver.
get
(
"https://cn.bing.com"
)
Get the page title
(
driver.
title
)
Close the browser
driver.
quit
(
)
Basic Usage
Initialize the WebDriver
Choose a browser and initialize the WebDriver:
Example
from
selenium
import
webdriver
Use the Chrome browser
driver
=
webdriver.
Chrome
(
)
Or use the Firefox browser
driver = webdriver.Firefox()
Or use the Edge browser
driver = webdriver.Edge()
Open a web page
Open a web page with the get() method:
driver.get("https://www.baidu.com")
Find page elements
Page elements can be found in several ways, for example by ID, class name or tag name:
Example
Find an element by ID
search_box
=
driver.
find_element
(
"id"
,
"kw"
)
Find an element by class name
search_button
=
driver.
find_element
(
"class name"
,
"s_ipt"
)
Find an element by tag name
links
=
driver.
find_elements
(
"tag name"
,
"a"
)
Simulate user actions
Selenium can simulate user actions in the browser, such as clicking and entering text:
Example
Enter text in the search box
search_box.
send_keys
(
"Selenium Python"
)
Click the search button
search_button.
click
(
)
Get element attributes and text
You can get an element's attribute values or text content:
Example
Get the element's text
element_text
=
search_box.
text
Get an element's attribute value
element_attribute
=
search_box.
get_attribute
(
"placeholder"
)
Waiting
Sometimes a page takes time to load; you can use explicit or implicit waits to make sure an element is actionable:
Example
from
selenium.
webdriver
.
common
.
by
import
By
from
selenium.
webdriver
.
support
.
ui
import
WebDriverWait
from
selenium.
webdriver
.
support
import
expected_conditions
as
EC
Explicit wait
element
=
WebDriverWait
(
driver
,
10
)
.
until
(
EC.
presence_of_element_located
(
(
By.
ID
,
"kw"
)
)
)
Implicit wait
driver.
implicitly_wait
(
10
)
Close the browser
When the operation is finished, remember to close the browser:
driver.quit()
Simple web automation
Below is a simple Selenium project example that automates searching for a keyword and gets the title of the results page.
Example
from
selenium
import
webdriver
from
selenium.
webdriver
.
common
.
by
import
By
Import the By module
from
selenium.
webdriver
.
chrome
.
service
import
Service
as
ChromeService
from
selenium.
webdriver
.
common
.
keys
import
Keys
from
selenium.
webdriver
.
support
.
ui
import
WebDriverWait
from
selenium.
webdriver
.
support
import
expected_conditions
as
EC
Set the driver path and start the browser
service
=
ChromeService
(
executable_path
=
"/RUNOOB/Downloads/chromedriver-mac-arm64/chromedriver"
)
options
=
webdriver.
ChromeOptions
(
)
driver
=
webdriver.
Chrome
(
service
=
service
,
options
=
options
)
try
:
Open the Baidu home page
driver.
get
(
"https://www.baidu.com"
)
Find the search box element
search_box
=
driver.
find_element
(
By.
ID
,
"kw"
)
Enter the search text
search_box.
send_keys
(
"Selenium Python"
)
Submit the search form
search_box.
send_keys
(
Keys.
RETURN
)
Wait for the search results to load
WebDriverWait
(
driver
,
10
)
.
until
(
EC.
presence_of_element_located
(
(
By.
ID
,
"content_left"
)
)
)
Print the page title
(
"Page title:"
,
driver.
title
)
finally
:
Close the browser
driver.
quit
(
)
Common selenium methods
The following table lists the common methods of the selenium library:
| Method | Description | Example Code |
|---|---|---|
webdriver.Chrome() |
Initialize a Chrome browser instance. | driver = webdriver.Chrome() |
driver.get(url) |
Visit a specified URL address. | driver.get("https://example.com") |
driver.find_element(By, value) |
Find the first matching element. | element = driver.find_element(By.ID, "id") |
driver.find_elements(By, value) |
Find all matching elements. | elements = driver.find_elements(By.CLASS_NAME, "class") |
element.click() |
Click an element. | element.click() |
element.send_keys(value) |
Send keyboard input to an input box. | element.send_keys("text") |
element.text |
Get an element's text content. | text = element.text |
driver.back() |
Navigate back in the browser. | driver.back() |
driver.forward() |
Navigate forward in the browser. | driver.forward() |
driver.refresh() |
Refresh the current page. | driver.refresh() |
driver.execute_script(script, *args) |
Execute a JavaScript script. | driver.execute_script("alert('Hello!')") |
driver.switch_to.frame(frame_reference) |
Switch to a specified iframe. | driver.switch_to.frame("frame_id") |
driver.switch_to.default_content() |
Switch back to the main document. | driver.switch_to.default_content() |
driver.quit() |
Close the browser and quit the driver. | driver.quit() |
driver.close() |
Close the current window. | driver.close() |