rust_drission 0.1.6

Browser automation library for Rust via Chrome DevTools Protocol (CDP). Connect or launch Chrome, control pages/elements, run JS, cookies, screenshots, request/response listening. API inspired by DrissionPage.
Documentation

rust_drission

rust_drission is a Rust browser automation library built on Chrome DevTools Protocol (CDP). Its public API is intentionally close to DrissionPage, and the recommended entry point for most usage is ChromiumPage.

rust_drission 是一个基于 Chrome DevTools Protocol (CDP) 的 Rust 浏览器自动化库。它的公开 API 尽量贴近 DrissionPage,大多数场景建议直接从 ChromiumPage 开始。

Why ChromiumPage

  • One entry point for launch/connect, navigation, element lookup, JS execution, screenshots, cookies, and tab access
  • DrissionPage-style locator strings such as css:, xpath:, text:, id:, class:, tag:
  • Supports direct JS/CDP calls when the high-level API is not enough
  • Keeps advanced capabilities available through page.tab() and page.browser()

Installation

[dependencies]
rust_drission = "0.1.5"

Quick Start

use rust_drission::{BrowserConfig, ChromiumPage, CdpError};

fn main() -> Result<(), CdpError> {
    let page = ChromiumPage::new(
        BrowserConfig::new()
            .headless(false)
            .set_local_port(9222),
    )?;

    page.get("https://example.com")?;
    println!("title = {}", page.title()?);
    println!("url = {}", page.url()?);

    if let Some(h1) = page.ele("css:h1")? {
        println!("h1 = {}", h1.text()?);
    }

    page.screenshot("example.png")?;
    Ok(())
}

Common Patterns

Launch a new Chrome

use rust_drission::{BrowserConfig, ChromiumPage};

let page = ChromiumPage::new(
    BrowserConfig::new()
        .chrome_path("C:\\Program Files\\Google\\Chrome\\Application\\chrome.exe")
        .user_data_dir("./data/profile")
        .set_local_port(9222)
        .headless(false),
)?;

Connect to an existing Chrome

Chrome must already be started with remote debugging enabled, for example:

chrome --remote-debugging-port=9222

Then connect:

use rust_drission::ChromiumPage;

let page = ChromiumPage::connect("127.0.0.1:9222")?;

Locate and operate on elements

page.input("css:input[name='q']", "rust cdp")?;
page.click("text:Search")?;

if let Some(button) = page.ele("id:submit")? {
    println!("enabled = {}", button.is_enabled()?);
}

Run JavaScript

let result = page.run_js("document.title")?;
let async_result = page.run_js_await("fetch('https://httpbin.org/get').then(r => r.json())")?;

Use advanced page APIs

use std::time::Duration;

let tab = page.tab();
tab.wait_visible("css:.result", Duration::from_secs(10))?;
tab.set_local_storage("token", "demo-value")?;

Documentation

Notes

  • ChromiumPage is the recommended starting point. Use page.tab() or page.browser() only when you need lower-level control.
  • Frame support is aimed at same-origin iframes.
  • listen() is available on Page, so with ChromiumPage you usually call page.tab().listen()?.
  • Element lookup methods often return Result<Option<Element>, CdpError>. Handle the None case explicitly.