杰瑞科技汇

Selenium for Python怎么用?入门指南或实战技巧?

Of course! Here is a comprehensive guide to using Selenium with Python, covering everything from setup to advanced techniques.

Selenium for Python怎么用?入门指南或实战技巧?-图1
(图片来源网络,侵删)

What is Selenium?

Selenium is a powerful open-source automation framework used primarily for testing web applications. It allows you to programmatically control a web browser, simulating user interactions like clicking buttons, filling out forms, navigating pages, and scraping data.

Think of it as giving a robot a pair of eyes and hands to browse the web exactly as a human would.


Prerequisites

  • Python 3: Make sure you have Python installed on your system. You can check by running python --version or python3 --version in your terminal.
  • pip: Python's package installer, which usually comes with Python.

Installation

First, you need to install the Selenium library for Python using pip. Open your terminal or command prompt and run:

pip install selenium

You'll also need a WebDriver. The WebDriver is a separate executable that acts as a bridge between your Python script and the web browser. You need one for each browser you want to automate.

Selenium for Python怎么用?入门指南或实战技巧?-图2
(图片来源网络,侵删)

Choosing a Browser and its WebDriver

The most popular choices are:

Browser WebDriver Name Download Link
Chrome chromedriver https://googlechromelabs.github.io/chrome-for-testing/
Firefox geckodriver https://github.com/mozilla/geckodriver/releases

How to Set Up the WebDriver (for Chrome):

  1. Find your Chrome version: Open Chrome, go to chrome://settings/help to see your version number (e.g., 0.6422.77).
  2. Download the matching chromedriver: Go to the Chrome for Testing page. Download the zip file for your Chrome version (the one that ends with -chromedriver.zip).
  3. Extract and place the driver:
    • Easy (for beginners): Unzip the file and place the chromedriver (or chromedriver.exe on Windows) executable in the same folder as your Python script.
    • Recommended (for everyone): Place the chromedriver executable in a directory that is already in your system's PATH. This way, you don't have to specify its location in your code. On macOS/Linux, you can put it in /usr/local/bin/.

Your First Script: "Hello, World!" of Web Automation

Let's write a simple script that opens Google, searches for "Selenium Python", and prints the page title.

# 1. Import the WebDriver
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.chrome.service import Service as ChromeService
from webdriver_manager.chrome import ChromeDriverManager # Optional, but recommended
# --- Optional but Recommended: Use webdriver-manager ---
# This automatically downloads and manages the correct chromedriver for you.
# No need to download it manually!
# driver = webdriver.Chrome(service=ChromeService(ChromeDriverManager().install()))
# --- Manual Setup (if you don't use webdriver-manager) ---
# Make sure chromedriver is in your PATH or provide the path
# service = ChromeService(executable_path='/path/to/your/chromedriver')
# driver = webdriver.Chrome(service=service)
# 2. Initialize the WebDriver
# This will open a new Chrome browser window.
driver = webdriver.Chrome()
# 3. Open a URL
driver.get("https://www.google.com")
# 4. Find an element and interact with it
# The search box has the name 'q'
search_box = driver.find_element(By.NAME, "q")
# Type "Selenium Python" into the search box
search_box.send_keys("Selenium Python")
# Press the Enter key
search_box.send_keys(Keys.RETURN)
# 5. Wait for the results to load and get the page title
# A small sleep can be useful, but explicit waits are better (see below)
# import time
# time.sleep(2) 
# Get the title of the current page= driver.title
print(f"Page Title is: {page_title}")
# 6. Close the browser
# driver.quit() is better as it closes the entire browser session.
driver.quit()

To run this script: Save it as first_script.py and run python first_script.py in your terminal. You should see a Chrome window open, perform the search, and then close.

Selenium for Python怎么用?入门指南或实战技巧?-图3
(图片来源网络,侵删)

Core Concepts

Finding Elements

This is the most important part of Selenium. You need to tell Selenium how to find the element you want to interact with. Selenium provides several strategies (By):

  • By.ID: The fastest and most reliable way. <div id="main-content">
  • By.NAME: Useful for form elements. <input name="username">
  • By.CLASS_NAME: Finds elements by their CSS class. <span class="highlight">
  • By.TAG_NAME: Finds elements by their HTML tag. <a>, <div>, <p>
  • By.CSS_SELECTOR: Very powerful and flexible. Finds elements using CSS selectors. div#main-content p.highlight
  • By.XPATH: Very powerful, uses path expressions to navigate the XML structure of the HTML. //div[@id='main-content']//p[@class='highlight']

Note: find_element returns the first match. find_elements (with an 's') returns a list of all matching elements.

Interacting with Elements

Once you've found an element, you can perform actions on it:

  • .send_keys("text"): Types text into an input field.
  • .click(): Clicks on a button or link.
  • .clear(): Clears the text from an input field.
  • .submit(): Submits a form.
  • .get_attribute("href"): Gets the value of an attribute (e.g., the URL of a link).

Handling Waits

Modern websites are dynamic. Elements load at different times. If your script tries to find an element before it's loaded, it will fail. This is where waits come in.

  • Implicit Wait: Tells WebDriver to poll the DOM for a certain amount of time when trying to find an element if it's not immediately available.

    driver.implicitly_wait(10) # Wait up to 10 seconds for any element to appear
    • Downside: It's a global setting and can slow down your script if not used carefully.
  • Explicit Wait (Recommended): Waits for a specific condition to be met before proceeding. This is much more reliable and efficient.

    from selenium.webdriver.support.ui import WebDriverWait
    from selenium.webdriver.support import expected_conditions as EC
    # Wait up to 10 seconds for the element with ID 'results' to be visible
    results_element = WebDriverWait(driver, 10).until(
        EC.visibility_of_element_located((By.ID, "results"))
    )

Advanced Example: Scraping Hacker News

Let's scrape the top 30 story titles and links from Hacker News.

from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
import time
driver = webdriver.Chrome()
driver.get("https://news.ycombinator.com")
# Use an explicit wait to ensure the story links are loaded
# Wait for at least one link with the class 'storylink' to be visible
try:
    # Wait for the first story link to be visible
    WebDriverWait(driver, 10).until(
        EC.visibility_of_element_located((By.CSS_SELECTOR, "a.storylink"))
    )
    # Find all story links
    # We use CSS_SELECTOR which is very efficient here
    story_links = driver.find_elements(By.CSS_SELECTOR, "a.storylink")
    story_scores = driver.find_elements(By.CSS_SELECTOR, "span.score")
    print("--- Top Hacker News Stories ---")
    # We'll get a max of 30 stories
    for i in range(min(30, len(story_links))):
        title = story_links[i].text
        link = story_links[i].get_attribute("href")
        # Get the corresponding score, if it exists
        score = 0
        if i < len(story_scores):
            score_text = story_scores[i].text
            score = int(score_text.replace(" points", ""))
        print(f"{i+1}. [{score} points] {title}")
        print(f"   Link: {link}\n")
except Exception as e:
    print(f"An error occurred: {e}")
finally:
    # Always close the driver
    driver.quit()

Best Practices

  1. Use driver.quit(): Always use driver.quit() at the end of your script. It closes all browser windows and ends the WebDriver session. driver.close() only closes the current window.
  2. Use Explicit Waits: Avoid time.sleep(). Explicit waits make your scripts more robust and faster because they only wait as long as necessary.
  3. Use try...finally: Wrap your automation logic in a try...finally block to ensure driver.quit() is always called, even if your script encounters an error.
  4. Use CSS Selectors or XPath: Learn to write good CSS selectors or XPath expressions. They are much more reliable than finding elements by class name or tag name alone.
  5. Headless Mode: For running scripts on a server (without a visible browser), you can run Chrome in "headless" mode.
    from selenium.webdriver.chrome.options import Options
    chrome_options = Options()
    chrome_options.add_argument("--headless") # Run in headless mode
    chrome_options.add_argument("--window-size=1920,1080") # Set a window size
    driver = webdriver.Chrome(options=chrome_options)
  6. Use a Page Object Model (POM): For large projects, organize your code using the Page Object Model. This is a design pattern where you create a separate class for each web page, encapsulating the locators and methods for interacting with that page. This makes your code much more maintainable.

Useful Resources

分享:
扫描分享到社交APP
上一篇
下一篇