SoFunction
Updated on 2025-04-22

Python's complete guide to obtaining web page information through Selenium

Selenium is a powerful automated testing tool, but it can also be used for crawling and analyzing web page information. This article will provide a detailed introduction to how to use Selenium to obtain web information and cover everything from environment building to advanced techniques.

1. Introduction

Selenium is a tool for automating browser operations. It supports multiple programming languages ​​(such as Python, Java, C#, etc.). Through Selenium, we can simulate users' behavior in the browser (such as clicking buttons, filling in forms, scrolling pages, etc.), thereby realizing the crawling and analysis of web page information.

Compared to the traditional combination of requests and BeautifulSoup, Selenium is more suitable for handling dynamically loaded content (such as JavaScript-rendered pages). Therefore, it is an important tool for obtaining information about complex web pages.

2. Environment construction

1. Install Python and Selenium

Before you start, make sure you have Python installed. Then, install Selenium using the following command:

pip install selenium

2. Download WebDriver

Selenium needs to be used in conjunction with the browser's WebDriver to run. The following are the WebDriver download addresses for common browsers:

ChromeDriver//driver/

GeckoDriver (Firefox)/mozilla/geckodriver/releases

EdgeDriver/en-us/microsoft-edge/tools/webdriver/

Add the downloaded WebDriver to the system environment variable, or specify its path in the code.

3. Example: Initialize the browser

Here is a simple sample code showing how to initialize a Chrome browser using Selenium:

from selenium import webdriver

# Initialize Chrome browserdriver = (executable_path='path/to/chromedriver')

# Visit the target page('')

3. Basic usage of Selenium

1. Visit the web page

(url)

Use the get method to access the specified URL.

2. Close the browser

# Close the current tab page()

# Exit the browser completely()

3. Set the waiting time

In some cases, the page loading may take a long time. This problem can be solved by setting implicit wait:

driver.implicitly_wait(10)  # Wait for 10 seconds

4. Positioning elements: Use of selectors

In Selenium, positioning elements is the core step in obtaining web page information. Selenium supports multiple selector methods:

1. ID selector

element = driver.find_element_by_id('element_id')

2. Name selector

element = driver.find_element_by_name('element_name')

3. Class selector

elements = driver.find_elements_by_class_name('class_name')  # Return all matching elements

4. CSS selector

element = driver.find_element_by_css_selector('#id .class') # Use CSS selector

5. XPath selector

XPath is a powerful selector language suitable for complex scenarios:

element = driver.find_element_by_xpath('//*[@]/div[@class="class"]')

6. Combination use

If none of the above methods can be located, it can be implemented in combination with various methods.

Example: Get the page title

title = 
print(title)

5. Obtain page information

1. Get element text

text = 
print(text)

2. Get element attributes

href = element.get_attribute('href')
print(href)

3. Process multiple elements

elements = driver.find_elements_by_css_selector('.class')  # Return to listfor elem in elements:
    print()

4. Extract the page source code

page_source = driver.page_source
print(page_source)

6. Handle dynamic content and wait

1. Explicitly wait

For dynamically loaded content, explicit waiting is a better choice:

from  import By
from  import WebDriverWait
from  import expected_conditions as EC

# Wait for the element to appearelement = WebDriverWait(driver, 10).until(
    EC.presence_of_element_located((, 'element_id'))
)

2. Implicit waiting

Implicit waits work globally and are not targeted at specific elements:

driver.implicitly_wait(10)  # Wait for 10 seconds

3. Handle dynamic content loading

For content that requires scrolling or clicking to display, you can use the following methods:

# Scroll to the bottom of the pagedriver.execute_script("(0, );")

# Click the Load More buttonload_more = driver.find_element_by_css_selector('.load-more')
load_more.click()

7. Common operation examples

Example 1: Log in to the system

from selenium import webdriver

driver = (executable_path='path/to/chromedriver')

# Visit the login page('/login')

# Enter username and passwordusername = driver.find_element_by_id('username')
password = driver.find_element_by_id('password')

username.send_keys('your_username')
password.send_keys('your_password')

# Click the login buttonlogin_button = driver.find_element_by_css_selector('.login-btn')
login_button.click()

# Close the browser()

Example 2: Submit the form

from selenium import webdriver

driver = (executable_path='path/to/chromedriver')

# Visit the form page('/form')

# Fill out the formname = driver.find_element_by_name('name')
email = driver.find_element_by_name('email')

name.send_keys('John Doe')
email.send_keys('@')

# Upload the file (if required)file_input = driver.find_element_by_css_selector('#file-input')
file_input.send_keys('/path/to/')

# Submit the formsubmit_button = driver.find_element_by_id('submit-btn')
submit_button.click()

()

Example 3: Get page information and save

from selenium import webdriver

driver = (executable_path='path/to/chromedriver')

# Visit the target page('')

# Get all linkslinks = driver.find_elements_by_css_selector('a[href]')
for link in links:
    print(link.get_attribute('href'))

# Save the page source code to the filewith open('page_source.html', 'w', encoding='utf-8') as f:
    (driver.page_source)

()

8. Case Study: From Simple to Complex

Case 1: Get news title

Suppose we need to extract the titles of all news from a news website:

from selenium import webdriver

driver = (executable_path='path/to/chromedriver')
('')

# Get all news titlestitles = driver.find_elements_by_css_selector('.news-title')
for title in titles:
    print()

()

Case 2: Handling paging

If the target page has paging, you can use a loop to grab data page by page:

from selenium import webdriver

driver = (executable_path='path/to/chromedriver')

for page in range(1, 6):  # Crawl the first 5 pages    (f'?page={page}')
    
    items = driver.find_elements_by_css_selector('.item')
    for item in items:
        print()
        
()

9. Summary

Through the above examples and case analysis, we can see Selenium's powerful capabilities in automated testing and data crawling. Combining technologies such as explicit waiting and dynamic content processing can deal with various complex scenarios.

Of course, the following points should be noted in practical applications:

Comply with the target website's documents.

Handle possible exceptions (such as element not found).

Use proxy IP and browser fingerprint disguise to avoid being intercepted by anti-crawl mechanisms.

This is the article about this article about Python’s complete guide to obtaining web page information through Selenium. For more related content related to Selenium, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!