Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

Selenium Installation

Selenium Installation

Before using Selenium, we need to install it first. The following are detailed steps for installing Selenium.

Selenium supports multiple programming languages, but this tutorial series will use Python as an example.

If you have not installed Python yet, you can install it by following these steps:

  1. Access the Python official website.
  2. Download the Python installer suitable for your operating system.
  3. Run the installer and make sure to check the "Add Python to PATH" option during installation.
  4. After installation, open the command line (Command Prompt or PowerShell on Windows, Terminal on macOS and Linux) and enter python --version or python3 --versionto check whether Python is installed successfully.

For Python content, you can also refer to:Python Tutorial

After installing Python, we can use pip to install Selenium.pip is Python's package management tool, used to install and manage Python libraries.

Open the command line and enter the following command to install Selenium:

pip install selenium

If you are using Python 3, you may need to use pip3:

pip3 install selenium

After installation, you can check whether Selenium is installed successfully with the following command:

pip show selenium

If the Selenium version information is displayed, the installation was successful.

Download the Browser Driver

Selenium needs a browser driver to control the browser.

Different browsers require different drivers; the following are the driver download addresses for common browsers:

Download the driver that matches your browser version, and add its path to the system environment variables, or place the driver file in the directory where your Python script is located.


Using Selenium

After installing Selenium and the browser driver, we can start writing automation scripts.

Here is a simple example showing how to use Selenium to open a web page and get the page title.

1. Import Selenium

First, we need to import Selenium's webdriver module:

from selenium import webdriver

2. Start the Browser

Next, we need to start a browser instance; this series will use Chrome as an example.

driver = webdriver.Chrome(executable_path='path/to/chromedriver')

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 using another browser such as Firefox, modify accordingly

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)

If you place chromedriver in the system path, or in the directory where your Python script is located, you can omit executable_path Parameters:

driver = webdriver.Chrome()

3. Open the Web Page

Use get method to open a web page:

driver.get('https://www.baidu.com')

4. Get the Page Title

We can use the title property to get the current page's title:

print(driver.title)

5. Close the Browser

After completing the operation, remember to close the browser:

driver.quit()

6. Complete Example

Combine the steps above; the complete code is as follows:

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://www.baidu.com")

# Get the page title
print(driver.title)

# Close the browser
driver.quit()

After the code runs successfully, it will open the browser and open the web page, as shown below:

After opening, it will output the web page title:

百度一下,你就知道

Use window.stop() to Force Stop Page Loading

Sometimes a web page loads too slowly and causes exceptions. To scrape data smoothly, you can let the page load for a few seconds and then execute the JavaScript code window.stop() to force stop page loading.

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://www.baidu.com")

# Wait for the page to finish loading
time.sleep(2)  # Adjust the wait time as needed

# Force stop page loading
driver.execute_script("window.stop();")
# Get the page title
print(driver.title)

# Close the browser
driver.quit()

Common Operations

Besides opening web pages and getting titles, Selenium supports many other operations. Here are some common operation examples:

1. Find Elements

You can use the find_element method to find elements on the page. For example, find an input box and enter text:

Example

input_element = driver.find_element('name', 'q')
input_element.send_keys('Selenium')

2. Click a Button

You can use the click method to click a button:

Example

button_element = driver.find_element('name', 'btnK')
button_element.click()

3. Wait for Page Loading

Sometimes page elements do not load immediately; we can use WebDriverWait to wait for the element to appear:

Example

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, 10)
element = wait.until(EC.presence_of_element_located((By.NAME, 'q')))