Skip to main content

BrowserSession

Struct BrowserSession 

Source
pub struct BrowserSession { /* private fields */ }
Expand description

A browser automation session backed by CDP.

Manages an optional child process (auto-spawned Lightpanda) and a direct CDP WebSocket connection for interaction.

The session is designed to be held behind Arc<tokio::sync::Mutex<Option<BrowserSession>>> for shared access across async tool calls. All public methods take &self.

§Process lifecycle

When created via launch(), the Lightpanda process is spawned with kill_on_drop(true) and additionally killed in the Drop impl. This ensures cleanup even if close() is not called.

Implementations§

Source§

impl BrowserSession

Source

pub async fn launch() -> Result<Self>

Spawn a Lightpanda process and connect to it via CDP.

  1. Finds a free TCP port by binding to 127.0.0.1:0
  2. Starts lightpanda serve --host 127.0.0.1 --port <port>
  3. Polls the port until CDP accepts connections (up to 10 seconds)
  4. Connects via WebSocket and opens a blank page

If lightpanda is not found on PATH, it is automatically downloaded from GitHub releases and installed to ~/.local/bin/.

§Errors
Source

pub async fn connect(cdp_url: &str) -> Result<Self>

Connect to an existing CDP endpoint.

Use this when the browser is managed externally (e.g. headless Chromium started by systemd, or a shared Lightpanda instance).

§Arguments
  • cdp_url — WebSocket URL (e.g. ws://127.0.0.1:9222/).
§Errors
Source

pub async fn navigate(&self, url: &str) -> Result<String>

Navigate to a URL. Returns the page title after load.

§Errors
Source

pub async fn extract(&self, selector: Option<&str>) -> Result<String>

Extract text content from the page or a specific element.

  • selector = None → returns document.body.innerText (full page text)
  • selector = Some("h1") → returns text of the first matching element
§Errors
Source

pub async fn click(&self, selector: &str) -> Result<()>

Click an element by CSS selector.

Dispatches a full MouseEvent (not just el.click()) so that framework event listeners (React, Vue, etc.) see the event. For submit buttons, also calls form.requestSubmit() to ensure form submission fires correctly.

§Errors
Source

pub async fn type_text(&self, selector: &str, text: &str) -> Result<()>

Type text into an element identified by CSS selector.

Uses the native HTMLInputElement value setter to bypass React’s internal value tracker, then dispatches input and change events so both React controlled inputs and plain HTML inputs update correctly.

§Errors
Source

pub async fn evaluate(&self, js: &str) -> Result<String>

Execute JavaScript on the page and return the result as a string.

String values are returned as-is. Numbers, booleans, objects, and arrays are serialized via serde_json::Value::to_string().

§Errors
Source

pub async fn url(&self) -> Result<String>

Get the current page URL.

Source

pub fn is_auto_spawned(&self) -> bool

Returns true if this session was auto-spawned (vs. connected to an external endpoint).

Source

pub async fn close(self) -> Result<()>

Close the browser session and kill the process if auto-spawned.

This is the preferred way to end a session. If not called, the Drop impl will still attempt to kill the child process, but cannot await its termination.

Trait Implementations§

Source§

impl Debug for BrowserSession

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Drop for BrowserSession

Source§

fn drop(&mut self)

Executes the destructor for this type. Read more

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

Source§

fn vzip(self) -> V

Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more