DrissionPage
DrissionPage is a Python-based web automation tool that efficiently combines browser control functionalities (similar to Playwright or Selenium) with high-performance data packet handling (akin to Requests). It provides a unified and user-friendly API for tasks like web scraping, automated testing, and general web interaction, allowing seamless transitions between full browser and headless HTTP modes. The library is actively maintained, currently at version 4.1.1.2, and aims for a balance of power, elegance, and ease of use, with a development cadence that includes frequent updates and feature enhancements.
Common errors
-
AttributeError: 'Browser' object has no attribute '_driver'
cause Attempting to use a `Browser` or `ChromiumPage` object across multiple threads without proper isolation.fixEnsure each thread initializes its own `ChromiumPage` instance. Browser objects are not thread-safe. -
DrissionPage.errors.ElementNotFoundError: 没有找到元素。
cause The specified element could not be found on the page within the default timeout. This often happens if the element is dynamic, not yet loaded, or the locator is incorrect.fixVerify your locator (CSS, XPath, etc.) and add explicit waits for the element to appear or be interactable (e.g., `page.wait.ele_loaded('css:.my-element')`). -
TimeoutError: timeout
cause The browser or a specific operation (like `page.get()` or an element interaction) exceeded its allotted time before completing, possibly due to slow network, heavy page content, or anti-bot measures.fixIncrease the timeout for the operation or the page object (e.g., `page.set.timeouts(page_load_timeout=30)`). Implement retry logic for unreliable operations.
Warnings
- breaking Version 3.0 introduced significant architectural changes, reducing reliance on Selenium and introducing the `WebPage` class. Code written for pre-3.0 `MixPage` or direct Selenium integration may break.
- gotcha When using DrissionPage in multi-threaded environments, direct use of `Browser` objects across threads can lead to `AttributeError: 'Browser' object has no attribute '_driver'` or other unexpected behavior due to non-thread-safe internal states.
- gotcha Element not found errors (`DrissionPage.errors.ElementNotFoundError`) are common, especially with dynamic content or incorrect locators. This can be due to the page not loading completely, elements not being present yet, or incorrect CSS/XPath selectors.
- gotcha Occasional timeout errors during page loading or `get()` requests can occur, particularly with unstable networks or heavily guarded websites.
- gotcha Opening multiple browser profiles simultaneously requires specific configuration and careful management, and may not behave as expected if not handled correctly.
Install
-
pip install drissionpage -
pip install drissionpage[chromium]
Imports
- ChromiumPage
from drissionpage.dp import ChromiumPage
from DrissionPage import ChromiumPage
- WebPage
from DrissionPage import MixPage
from DrissionPage import WebPage
- SessionPage
from DrissionPage.dp import SessionPage
from DrissionPage import SessionPage
Quickstart
from DrissionPage import ChromiumPage
# Initialize a browser page (Chromium is default)
page = ChromiumPage()
# Navigate to a website
page.get('https://www.example.com')
# Print the title of the page
print(f"Page Title: {page.title}")
# Find an element by CSS selector and get its text
element = page.ele('css:h1')
if element:
print(f"H1 Text: {element.text}")
# Close the browser
page.quit()