SoFunction
Updated on 2025-04-13

Three modes to simplify Python browser automation

introduction

In today's network data processing and automation tasks, browser automation has become an indispensable technology. To facilitate Python developers to operate web pages more efficiently, DrissionPage provides a lightweight and powerful solution.

This article will provide an in-depth explanation of the three core modes of DrissionPage: ChromiumPage, WebPage and SessionPage, and introduce in detail how to choose the appropriate mode in different application scenarios. Whether it is necessary to simulate graphical user operations, control request packets, or implement fast interfaceless data interactions, DrissionPage provides flexible support.

1. Introduction to DrissionPage

  • DrissionPage is a Python-based browser automation library, similar to Selenium and Playwright, and is mainly used to simplify web page operations.
  • It provides three access modes: ChromiumPage, WebPage, and SessionPage.
  • Each mode has different uses and can usually serve as an entry to the program. The following is a brief introduction and usage description of these three modes:

①ChromiumPage

  • ChromiumPage is a page object specially used to operate browsers, mainly using the Chromium browser kernel for page loading and interaction. It is similar to Selenium's WebDriver, which supports search, click, input, screenshot and other operations of page elements.

Common usage examples

First install the DrissionPage:

pip install drission 
  • Then, you can use the following code example to open a page and do a simple operation.
from drission 
import Drission, ChromiumPage   # Initialize the Drission objectdrission = Drission()   # Create a ChromiumPage object and open a browser pagepage = drission.use_chromium()   # Open the specified URL("")  # Find elements and operate('button[]')  # Click the button('input[name="username"]', 'your_username')  # Enter text# Save screenshot(path="")  # Close the browser()

In ChromiumPage, you can do the following:

  • get(url): Open the specified URL.
  • click(selector): Click on the page element through the selector.
  • input(selector, text): Enter the specified text in the input box.
  • screenshot(path="filename"): Take a screenshot of the page.
  • close(): Close the current page and browser.

ChromiumPage is suitable for handling web page automation tasks that require graphical interface display, and can simulate user interaction with pages.

②WebPage

  • WebPage is a relatively complex page object in DrissionPage, combining browser control and packet sending and receiving. WebPage is mainly used in situations where the browser needs to control and process requests and response packets at the same time. It can intercept and modify network requests, and can directly process AJAX requests when interacting with pages.
  • Although WebPage is more powerful, if you don't need to focus on learning it, you can use ChromiumPage or SessionPage directly for simple browser automation or data requests.

Example usage:

from drission 
import WebPage
page = WebPage()
("")  # Use WebPage to control pages and process network requests

③SessionPage

  • SessionPage is a page object dedicated to packet sending and receiving, mainly used to simulate HTTP requests, similar to Python's requests library. It does not depend on the browser and is therefore suitable for tasks that require quick sending and receiving data.
  • SessionPage is ideal for accessing APIs, downloading data, or interacting with servers without actually rendering web content.

Example of usage

from drission 
import SessionPage    # Create a SessionPage objectpage = SessionPage()  # Send a GET requestresponse = ("/api")
print()  # Send a POST requestdata = {'username': 'user', 'password': 'pass'}
response = ("/login", data=data)
print(())

In SessionPage, commonly used methods include:

  • get(url, params=None): Send a GET request.
  • post(url, data=None, json=None): Send a POST request.
  • json(): parses the response to JSON format.
  • text: Get the response text.
  • SessionPage is suitable for lightweight data interaction tasks without rendering web pages.

④Summary of the three major modules

  • ChromiumPage: Suitable for graphical browser automation and supports rich page interaction
  • WebPage: Integrates browser operations and packet sending and receiving, suitable for complex tasks that require simultaneously operating the browser and processing packets (usually not commonly used).
  • SessionPage: Only used to send HTTP requests, similar to requests, suitable for lightweight data acquisition and API interaction.

2. ChromiumPage

  • ChromiumPage is a page object in the DrissionPage library that is specifically used for browser automation.
  • It is based on the Chromium browser kernel and is used to open web pages and perform various interactions on them.
  • ChromiumPage provides functions similar to Selenium, allowing us to search, click, enter text, screenshot and other operations on elements in web pages, which is suitable for automated tasks that require interface.

①Initialize ChromiumPage

  • To use ChromiumPage, you first need to install DrissionPage and related dependencies:
pip install drission
  • The basic process of using ChromiumPage is as follows:
from drission 
import Drission   # Create a Drission objectdrission = Drission()  # Initialize ChromiumPage through a Drission objectpage = drission.use_chromium()

②Basic operation

1. Open the page

Use the get() method to load a page:

("")

2. Find elements

  • ChromiumPage provides a variety of ways to find elements, commonly used methods include:
    Single element search: Use ele() to find a single element.
    Multiple element search: Use eles() to find all elements that meet the criteria.
# Find a single elementelement = ('button#submit') # Find multiple elementselements = ('')

3. Click on the element

  • Use the click() method to click on elements in the page:
('button#submit') # Find and click the button through selector

4. Enter text

  • Use the input() method to enter text into an input box or text box:
('input#username', 'your_username')

5. Obtain text content

  • Use text() or ele() in combination with .text attributes to get the text content of an element:
text = ('').text

6. Screenshot

  • Use the screenshot() method to screenshot a page or a specific element:
(path="")  # Screenshot of the entire page ('').screenshot(path="element_screenshot.png") # Screenshot of the element

7. Wait for the element to load

  • ChromiumPage provides automatic waiting function for scenarios where page elements need to be loaded. You can control the waiting behavior through wait() and other methods:
# Wait for a specific element to appear('div#loaded_element')

You can specify the waiting timeout and conditions to ensure that subsequent operations are performed after the element is loaded.

8. Execute JavaScript scripts

  • Sometimes you need to execute custom JavaScript scripts. ChromiumPage supports executing JavaScript through the run_js() method and obtaining the results:
# Execute JavaScript on the pageresult = page.run_js('return ')
print(result)  # Output page title

9. Control browser options

  • When starting ChromiumPage, you can configure the browser's startup parameters through the launch method. For example, headless mode, window size, etc.:
page = drission.use_chromium(headless=False, args=["--window-size=1024,768"])

Parameter description:

  • headless: Set to False to display the browser window, True in headless mode (for background operation)
  • args: Set startup parameters, such as specifying the window size, disabling extensions, etc.

10. Wait for the page to load

  • For dynamic pages or pages with more AJAX content loading, you can use the wait_load() method to wait for the page to fully load.
("")
page.wait_load()  # Wait for the page to load

11. Close the browser

  • After all operations are completed, close ChromiumPage using the close() method to free up the resource:
()

12. Use ChromiumPage to complete a simple login operation

  • Here is a sample code showing how to automate login with ChromiumPage, fill in forms, and click on buttons.
from drission 
import Drission  # Create a Drission object and use ChromiumPagedrission = Drission()
page = drission.use_chromium()  # Open the login page("")    # Enter username and password("#username", "your_username")
("#password", "your_password") # Click the login button("#login_button") # elements waiting for login to appear('-message')  # Get welcome information after loginwelcome_text = ('-message').text
print("Welcome message:", welcome_text)  # Close the browser ``()

Summarize

  • ChromiumPage provides a rich set of APIs that allow us to easily automate tasks of browsers.
  • Supported operations include: opening the page, finding elements, clicking, entering, taking screenshots, waiting for elements to load, executing JavaScript, etc.
  • ChromiumPage allows easy control of the Chromium browser, suitable for automated tasks that require interactive operations.

3. Summary

  • As a simple and easy-to-use browser automation library, DrissionPage brings great convenience to Python developers.
  • Through ChromiumPage's graphical interface operation, WebPage's network request control capabilities, and SessionPage's lightweight data interaction, developers can flexibly deal with a variety of web page automation tasks.
  • Whether it is to implement complex page operations or to conduct efficient interface data interaction, DrissionPage is a rare weapon that allows you to easily control every step of web page automation.

The above is the detailed content of the three modes that simplify Python browser automation. For more information on simplifying Python browser automation, please pay attention to my other related articles!