Python Selenium
## Python3.x 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 purposes. The current official API documentation includes C#, JavaScript, Java, Python, and Ruby.
**Selenium Tutorial: [
* * *
## Install Selenium and WebDriver
### Install 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:
```python
pip install selenium
After installation, you can use the following command to view selenium version information:
```python
pip show selenium
You can also view it using Python code:
```python
import selenium
print(selenium.__version__)
### Download 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 for your browser and ensure it is in your system PATH.
* Chrome: (https://developer.chrome.com/docs/chromedriver/downloads?hl=zh-cn)
* Firefox: (https://github.com/mozilla/geckodriver/releases)
* Edge: (https://developer.microsoft.com/en-us/microsoft-edge/tools/webdriver/)
* Safari: (https://webkit.org/blog/6900/webdriver-support-in-safari-10/)
Select a browser and initialize the WebDriver:
## Example
```python
from selenium import webdriver
# Use Chrome browser
driver = webdriver.Chrome(executable_path='/path/to/chromedriver')
# Or use Firefox browser
# driver = webdriver.Firefox(executable_path='/path/to/geckodriver')
# Or use Edge browser
# driver = webdriver.Edge(executable_path='/path/to/msedgedriver')
**Starting from Selenium 4, there have been changes in the way browser drivers are managed: Selenium 4 attempts to automatically detect the browser version installed on the system and download the corresponding driver, which means users no longer need to manually download and set the driver path unless they need a specific version of the driver.**
## Example
```python
from selenium import webdriver
driver = webdriver.Chrome()# If using other browsers like Firefox, modify accordingly
When the domestic network environment, automatic detection and driver download requires a different network environment, so it is recommended to manually download the driver and then specify the driver path.
In Selenium 4, instead of directly setting the driver path in webdriver.Chrome, you set it by introducing a Service object. This avoids deprecation warnings and ensures proper loading of the driver. For example:
## Example
```python
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 to get the web page title:
## Example
```python
from selenium import webdriver
from selenium.webdriver.chrome.service import Service as ChromeService
# Set the correct driver path
service = ChromeService(executable_path="/TUTORIAL/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
print(driver.title)
# Close the browser
driver.quit()
* * *
## Basic Usage
### Initialize WebDriver
Select a browser and initialize the WebDriver:
## Example
```python
from selenium import webdriver
# Use Chrome browser
driver = webdriver.Chrome()
# Or use Firefox browser
# driver = webdriver.Firefox()
# Or use Edge browser
# driver = webdriver.Edge()
### Open a Web Page
Use the get() method to open a web page:
```python
driver.get("https://www.baidu.com")
### Find Page Elements
You can find page elements in various ways, such as using ID, class name, tag name, etc.:
## Example
```python
# Find element by ID
search_box = driver.find_element("id","kw")
# Find element by class name
search_button = driver.find_element("class name","s_ipt")
# Find elements by tag name
links = driver.find_elements("tag name","a")
### Simulate User Operations
Selenium can simulate user operations in the browser, such as clicking, entering text, etc.:
## Example
```python
# 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 the attribute value or text content of page elements:
## Example
```python
# Get the text of the element
element_text = search_box.text
# Get the attribute value of the element
element_attribute = search_box.get_attribute("placeholder")
### Wait
Sometimes page loading takes time, you can use explicit wait or implicit wait to ensure the element is actionable:
## Example
```python
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
After completing operations, remember to close the browser:
```python
driver.quit()
* * *
## Simple Web Automation
Below is a simple Selenium project example, used to automate searching for keywords and get the title of the results page.
## Example
```python
from selenium import webdriver
from selenium.webdriver.common.by import By # Import 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="/TUTORIAL/Downloads/chromedriver-mac-arm64/chromedriver")
options = webdriver.ChromeOptions()
driver = webdriver.Chrome(service=service, options=options)
try:
# Open Baidu homepage
driver.get("https://www.baidu.com")
# Find the search box element
search_box = driver.find_element(By.ID,"kw")
# Enter search content
search_box.send_keys("Selenium Python")
# Submit the search form
search_box.send_keys(Keys.RETURN)
# Wait for search results to load
WebDriverWait(driver,10).until(
EC.presence_of_element_located((By.ID,"content_left"))
)
# Print the page title
print("Page title is:",
YouTip