SoFunction
Updated on 2025-04-13

Python uses ChromiumPage in DrissionPage to automate web page operations

Preface

With the increasing demand for network automation, Python developers need a simple and efficient tool to implement browser control and web operations. As a lightweight and powerful browser automation library, DrissionPage provides developers with rich functional support. This article will focus on ChromiumPage in DrissionPage, covering the core functions from basic browser startup, element operations to iframe switching. Whether it is the advancement of beginners or the advanced needs of senior users, this article will provide you with practical reference guides to help you easily control web automation tasks.

1. Basic operations of ChromiumPage

In DrissionPage, ChromiumPage provides a variety of configuration options to customize the browser's startup method, supporting headless mode, proxy settings, custom window size and other configurations. The following are detailed actions for opening the browser and initializing the configuration in ChromiumPage.

1. Initialize Drission and ChromiumPage objects

The first step in opening a browser with ChromiumPage is to create a Drission object and then use it to initialize ChromiumPage. This is the standard process for DrissionPage to perform browser automation.

from drission import Drission
 
# Create a Drission objectdrission = Drission()
 
# Initialize ChromiumPage with Drission objectpage = drission.use_chromium()

2. Open the browser and load the page

After creating a ChromiumPage, you can use the get() method to load the specified URL, which opens the web page in the browser.

# Open the web page("")

3. Set browser startup parameters

ChromiumPage supports customization of multiple configuration parameters at startup, and the following are commonly used configuration items:

headless: Whether to enable headless mode. Headless mode does not display the browser window, which is suitable for running automated tasks in the background. Default is True.

args: Additional startup parameters that can pass any Chromium-supported startup options such as window size, disable extensions, etc.

proxy: Sets the IP and port of the proxy server to modify the access IP address.

user_agent: Customize the User-Agent ID of the browser.

disable_image: Disable image loading, suitable for increasing loading speed without the need for images.

The following example shows how to configure these parameters at startup:

# Start the browser and set the initialization configurationpage = drission.use_chromium(
    headless=False,                        # Whether to enable headless mode    args=["--window-size=1200,800"],       # Set the window size    proxy="127.0.0.1:8080",                # Set up a proxy    user_agent="MyCustomUserAgent",        # Custom User-Agent    disable_image=True                     # Disable image loading to speed up page loading)

4. Loading waiting settings

For pages that need to wait for dynamic content to load, you can use the wait_load() method to ensure that the page resources are fully loaded before continuing to perform other operations. This avoids operation failure issues caused by unloading.

("")
page.wait_load()  # Wait for the page to load

2. ChromiumPage element operation

In the ChromiumPage of DrissionPage, element positioning is a key feature that finds and operates on elements in a page. ChromiumPage provides a variety of element positioning methods, similar to how Selenium operates. The following are several common methods of element positioning.

1. Basic element positioning method

The element positioning methods provided by ChromiumPage mainly include the following:

ele(selector): Position a single element

eles(selector): Position multiple elements and return a list of elements

In these methods, the selector is a selector used to specify HTML elements and supports multiple selector types (such as CSS selector, XPath).

2. Commonly used selector types

In the ele() and eles() methods, the following selector types are supported:

CSS selector: Use CSS style selector to locate elements, common forms include:

  • Tag name: 'div'
  • Class name: ''
  • ID:'#element_id'
  • Attribute: 'input[name="username"]'

XPath: Position elements through XPath expression. For example, '//div[@class="classname"]' can locate a div element with a classname.

Tag attribute: You can directly use the form of @attribute=value, such as @id='element_id'.

The following is the specific example code:

# Positioning a single element through a CSS selectorelement = ('button#submit')
 
# Position multiple elements by class name and return the element listelements = ('')
 
# Position elements using XPathelement = ('//input[@name="username"]')

3. Common element operation methods

After positioning an element, you can perform further operations on the element. The following are the commonly used element operation methods:

Click element: Use the click() method to click on the positioned element.

('button#submit') # Find and click the button through selector

Enter text: Use the input() method to enter the text into the specified input box.

('input#username', 'your_username')

Get text content: Use text() to get the text content of the element.

text = ('').text
print(text)

Get attribute value: Use attr() to get the specified attribute value of the element.

attribute_value = ('img').attr('src')
print(attribute_value)

Check whether an element is visible: You can use the exists() method to check whether an element exists or is visible.

if ('').exists():
    print("Element is visible")

4. Example: Complete element operation process

The following sample code shows how to use elements to locate, enter text, click buttons, and get text content:

from drission import Drission
 
# Create Drission and ChromiumPage objectsdrission = Drission()
page = drission.use_chromium(headless=False)
 
# Open the web page("")
 
# Locate and enter text('input#username', 'your_username')
('input#password', 'your_password')
 
# Position and click the button('button#login_button')
 
# Wait for the page to load and get a welcome message('-message')
welcome_text = ('-message').text
print("Welcome message:", welcome_text)
 
# Close the browser()

5. Element operation summary

DrissionPage's ChromiumPage provides a simple element positioning and operation method. Users can locate page elements through CSS selector or XPath, and click, enter, and obtain text. Combined with the element waiting mechanism, dynamic content can be better handled and stable browser automation tasks can be completed.

IFrame switching

In web page automation, an iframe is an element used to embed other HTML documents into the page. Many web pages embed functionality or content into an iframe, and you need to switch to the iframe before directly manipulating elements in an iframe. In DrissionPage, ChromiumPage provides a way to switch to an iframe, allowing users to easily access and manipulate content within an iframe.

1. Why do I need to switch iframes

By default, the browser is in the context of the main page when loading the page, but the iframe content is in a different document environment. Therefore, if you want to operate elements in an iframe, you must first switch the browser context to the corresponding iframe. Otherwise, finding elements in the iframe will fail.

2. Use the to_iframe() method to switch to the specified iframe

ChromiumPage provides the to_iframe() method for switching to an iframe. This method allows you to specify the iframe to switch through the sequence number or selector of the iframe.

Example:

# Suppose there is an iframe in the page and the selector is iframe#my_iframepage.to_iframe('iframe#my_iframe') # Switch to iframe via selector

Switch by serial number: If the page has multiple iframes, you can switch to a specific iframe through indexing, for example page.to_iframe(0) will switch to the first iframe.

Switch through selector: You can use a selector (such as iframe#my_iframe) to switch to the specified iframe.

3. Manipulate elements in iframe

After switching to an iframe, you can directly find and manipulate elements in the iframe. For example:

# Suppose there is a button inside the iframe with the ID submit_buttonpage.to_iframe('iframe#my_iframe') # Switch to iframe('#submit_button') # Operate buttons in iframe

4. Switch back to the main page

After completing the operation within the iframe, you can use the to_parent() method to switch back to the main page.

page.to_parent()  # Switch back to the main page

5. Complete example

Here is an example showing how to manipulate elements in an iframe and switch back to the main page:

from drission import Drission
 
# Initialize Drission and ChromiumPagedrission = Drission()
page = drission.use_chromium()
 
# Open a page containing an iframe("")
 
# Switch to an iframe and manipulate the elements in itpage.to_iframe('iframe#iframe_id') # Switch to iframe via selector('#input_field', 'some text') # Enter text in the input box inside the iframe('#submit_button') # Click the button inside the iframe 
# Switch back to the main page and operate other elementspage.to_parent()
('#main_page_button') # Operate the button for the main page 
# Close the browser()

Switch summary

The to_iframe() method of ChromiumPage allows you to easily switch between an iframe and a main page to access and manipulate content nested within an iframe. After completing the operation, you can use the to_parent() method to switch back to the main page to facilitate the processing of other elements of the main page. This switching mechanism is suitable for automation scenarios of various nested pages.

4. Summary

Through the ChromiumPage of DrissionPage, developers can easily implement browser automation control. Whether it is to accurately find elements through a diverse element positioning method, switch to nested iframes to achieve multi-level operations, or set headless mode and proxy to customize startup, ChromiumPage provides rich support. Its simple interface and powerful functions make complex automation tasks simple and easy to operate, making it the best choice to improve web page operation efficiency. I hope this article's explanation will help you understand and apply DrissionPage further, making web page automation tasks easier and more efficient.

The above is the detailed content of Python using ChromiumPage in DrissionPage to automate web operations. For more information about Python's automated web operations, please pay attention to my other related articles!