Python selenium Library

Selenium is a powerful tool for automating web browser operations, widely used in web application testing, web data scraping, and task automation scenarios.
Selenium provides APIs for various programming languages for testing. The current official API documentation includes C#, JavaScript, Java, Python, and Ruby.
Selenium Tutorial: https://www.runoob.com/selenium/selenium-tutorial.html
Installing Selenium and WebDriver
Section titled “Installing Selenium and WebDriver”Installing Selenium
Section titled “Installing Selenium”To start using Selenium, you first need to install the selenium library and download the WebDriver suitable for your browser.
Install Selenium using pip:
After installation, you can use the following command to check the selenium version information:
You can also check using Python code:
Downloading WebDriver
Section titled “Downloading WebDriver”Selenium requires a WebDriver to interact with the browser.
Different browsers require different WebDrivers. For example, Chrome browser requires ChromeDriver. You need to download the corresponding WebDriver based on the browser you are using and ensure it is in your system PATH.
- Chrome: ChromeDriver
- Firefox: GeckoDriver
- Edge: EdgeDriver
- Safari: SafariDriver
Select a browser and initialize WebDriver:
Example
Section titled “Example”Starting from Selenium 4, the way browser drivers are managed has changed: Selenium 4 attempts to automatically detect the installed browser version on the system and download the corresponding driver. This means users no longer need to manually download and set the driver path, unless they need a specific version of the driver.
Example
Section titled “Example”In domestic network environments, automatic detection and driver download require different network conditions, so it is recommended to manually download the driver and specify the driver path.
In Selenium 4, you no longer set the driver path directly in webdriver.Chrome. Instead, you introduce the Service object to set it. This avoids deprecation warnings and ensures correct driver loading. For example:
Example
Section titled “Example”The following code specifies the driver file path to get the page title:
Example
Section titled “Example”Basic Usage
Section titled “Basic Usage”Initializing WebDriver
Section titled “Initializing WebDriver”Select a browser and initialize WebDriver:
Example
Section titled “Example”Opening a Web Page
Section titled “Opening a Web Page”Use the get() method to open a web page:
Finding Page Elements
Section titled “Finding Page Elements”You can find page elements in various ways, such as using ID, class name, tag name, etc.:
Example
Section titled “Example”Simulating User Actions
Section titled “Simulating User Actions”Selenium can simulate user actions in the browser, such as clicking, entering text, etc.:
Example
Section titled “Example”Getting Element Attributes and Text
Section titled “Getting Element Attributes and Text”You can get the attribute values or text content of page elements:
Example
Section titled “Example”Waiting
Section titled “Waiting”Sometimes page loading takes time. You can use explicit waits or implicit waits to ensure elements are operable:
Example
Section titled “Example”Closing the Browser
Section titled “Closing the Browser”After completing operations, remember to close the browser:
Simple Web Automation
Section titled “Simple Web Automation”The following is a simple Selenium project example for automating keyword searches and getting the title of the results page.
Example
Section titled “Example”selenium Common Methods
Section titled “selenium Common Methods”The following table lists common methods of the selenium library:
| Method | Description | Example Code |
|---|---|---|
webdriver.Chrome() |
Initialize a Chrome browser instance. | driver = webdriver.Chrome() |
driver.get(url) |
Visit the 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 field. | element.send_keys("text") |
element.text |
Get the text content of an element. | text = element.text |
driver.back() |
Browser back. | driver.back() |
driver.forward() |
Browser forward. | driver.forward() |
driver.refresh() |
Refresh the current page. | driver.refresh() |
driver.execute_script(script, *args) |
Execute JavaScript script. | driver.execute_script("alert('Hello!')") |
driver.switch_to.frame(frame_reference) |
Switch to the 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() |