Skip to main content

CdpClient

Struct CdpClient 

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

A single WebSocket session to one Chrome debugging target.

Cloning is not supported; share a client across tasks with Arc<CdpClient> (see Cluster for an example). Every command sent concurrently gets its own response future, matched by CDP message id.

Implementations§

Source§

impl CdpClient

Source

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

Open a CDP WebSocket session at ws_url (a target’s webSocketDebuggerUrl).

Spawns background tasks that pump outgoing commands and demultiplex incoming replies/events for the lifetime of the returned client.

Source

pub fn set_command_timeout(&self, timeout: Duration)

Set the per-command timeout applied to every CDP command this client sends. Passing a zero duration disables the timeout (commands wait indefinitely).

Source

pub fn command_timeout(&self) -> Duration

The current per-command timeout.

Source

pub fn subscribe_events(&self) -> Receiver<(String, Value)>

Subscribe to raw CDP events as (method, params) pairs.

Only events for domains enabled via enable_domain are emitted. Lagging receivers silently drop the oldest events rather than blocking the connection.

Source

pub async fn wait_for_event( &self, method: &str, timeout_ms: u64, ) -> Result<Value>

Wait for the next event named method, up to timeout_ms milliseconds.

Source

pub async fn enable_domain(&self, domain: &str) -> Result<()>

Enable a CDP domain (e.g. "Page", "Runtime", "Network"), required before that domain’s events start flowing or some of its commands work.

Source

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

Navigate to url. Returns as soon as navigation starts, without waiting for the page to finish loading; use navigate_and_wait to block until Page.loadEventFired.

Source

pub async fn navigate_and_wait( &self, url: &str, timeout_ms: u64, ) -> Result<NavigationResult>

Navigate to url and wait for Page.loadEventFired, up to timeout_ms. Requires the "Page" domain to be enabled.

Source

pub async fn eval(&self, expression: &str) -> Result<String>

Evaluate a JS expression and return its result stringified. Prefer evaluate when you need the structured result or exception details.

Source

pub async fn evaluate(&self, expression: &str) -> Result<EvaluateResult>

Evaluate a JS expression via Runtime.evaluate and return the full result, including any exception details.

Source

pub async fn get_document(&self) -> Result<DocumentNode>

Fetch the document’s root node. Requires the "DOM" domain to be enabled.

Source

pub async fn query_selector( &self, node_id: i64, selector: &str, ) -> Result<Option<i64>>

Returns the matched nodeId, or None when nothing matches. CDP reports a missing match as nodeId 0; this surfaces that as None rather than a node id that looks valid.

Source

pub async fn get_outer_html(&self, node_id: i64) -> Result<String>

Serialize a node’s outer HTML.

Source

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

Close the tab this client is attached to.

Source

pub async fn screenshot(&self) -> Result<Vec<u8>>

Capture a PNG screenshot of the current viewport.

Source

pub async fn screenshot_to_file(&self, path: &str) -> Result<()>

screenshot, written directly to path.

Source

pub async fn full_page_screenshot(&self) -> Result<Vec<u8>>

Capture a PNG screenshot of the full page, resizing the viewport to the page’s scroll size first (restoring it is the caller’s responsibility).

Source

pub async fn full_page_screenshot_to_file(&self, path: &str) -> Result<()>

full_page_screenshot, written directly to path.

Source

pub async fn set_viewport( &self, width: i32, height: i32, mobile: bool, ) -> Result<()>

Override the viewport size and mobile emulation flag.

Source

pub async fn get_cookies(&self) -> Result<Vec<Cookie>>

List cookies visible to the current page.

Source

pub async fn get_version(host: &str, port: u16) -> Result<BrowserVersion>

GET /json/version on Chrome’s debugging HTTP endpoint.

Source

pub async fn list_targets(host: &str, port: u16) -> Result<Vec<Target>>

GET /json/list: enumerate debuggable targets (tabs, workers, …).

Source

pub async fn connect_to_page(host: &str, port: u16) -> Result<Self>

Connect to the first target of type "page" reported by list_targets. Returns CdpError::NoTarget if none exists.

Source

pub async fn create_tab( host: &str, port: u16, url: Option<&str>, ) -> Result<Target>

Open a new tab, optionally navigating it to url immediately. Uses PUT /json/new since modern Chrome rejects GET for this endpoint.

Source§

impl CdpClient

Set a cookie. url or domain is required by CDP to resolve which site the cookie belongs to.

Source

pub async fn delete_cookies(&self, name: &str, url: Option<&str>) -> Result<()>

Delete cookies matching name (and optionally scoped to url).

Source

pub async fn set_extra_headers( &self, headers: &HashMap<String, String>, ) -> Result<()>

Send headers with every subsequent request from this target.

Source

pub async fn block_urls(&self, patterns: &[&str]) -> Result<()>

Block requests whose URL matches any of patterns (CDP wildcard syntax, e.g. "*.example.com/*").

Source

pub async fn get_response_body(&self, request_id: &str) -> Result<String>

Fetch a response body by request id, decoding it if Chrome returned it base64-encoded.

Source

pub async fn intercept_requests(&self, url_patterns: &[&str]) -> Result<()>

Enable request interception for URLs matching url_patterns. Matching requests pause and must be resolved with continue_request or fulfill_request, delivered as Fetch.requestPaused events (see subscribe_events).

Source

pub async fn continue_request(&self, request_id: &str) -> Result<()>

Let a paused request proceed unmodified.

Source

pub async fn fulfill_request( &self, request_id: &str, status: u16, body: &str, content_type: &str, ) -> Result<()>

Respond to a paused request with a synthetic response instead of letting it reach the network.

Source§

impl CdpClient

Source

pub async fn set_content(&self, html: &str) -> Result<()>

Replace the current document’s content with html.

Source

pub async fn print_to_pdf(&self, path: &str) -> Result<()>

Render the current page to PDF and write it to path.

Source

pub async fn add_init_script(&self, source: &str) -> Result<String>

Register source to run before every future document on this target (before any page script runs). Returns an identifier for remove_init_script.

Source

pub async fn remove_init_script(&self, identifier: &str) -> Result<()>

Unregister a script added via add_init_script.

Source

pub async fn set_user_agent(&self, ua: &str) -> Result<()>

Override the User-Agent header and navigator.userAgent.

Source

pub async fn set_geolocation( &self, latitude: f64, longitude: f64, accuracy: f64, ) -> Result<()>

Override the geolocation API’s reported position.

Source

pub async fn set_offline(&self, offline: bool) -> Result<()>

Simulate going offline (or restore normal networking).

Source

pub async fn set_attribute( &self, node_id: i64, name: &str, value: &str, ) -> Result<()>

Set (or add) an attribute on a DOM node.

Source

pub async fn set_outer_html(&self, node_id: i64, html: &str) -> Result<()>

Replace a DOM node’s outer HTML.

Source

pub async fn remove_node(&self, node_id: i64) -> Result<()>

Remove a DOM node from the document.

Source

pub async fn call_function_on( &self, object_id: &str, function_declaration: &str, ) -> Result<Value>

Call function_declaration with the object identified by object_id as this, returning its result by value.

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<ST, DT> CastableFrom<ST, Initialized, Initialized> for DT
where ST: ?Sized, DT: ?Sized,

Source§

impl<ST, DT> CastableFrom<ST, Uninit, Uninit> for DT
where ST: ?Sized, DT: ?Sized,

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> PolicyExt for T
where T: ?Sized,

Source§

fn and<P, B, E>(self, other: P) -> And<T, P>
where T: Sized + Policy<B, E>, P: Policy<B, E>,

Create a new Policy that returns Action::Follow only if self and other return Action::Follow. Read more
Source§

fn or<P, B, E>(self, other: P) -> Or<T, P>
where T: Sized + Policy<B, E>, P: Policy<B, E>,

Create a new Policy that returns Action::Follow if either self or other returns Action::Follow. Read more
Source§

impl<T> Read<Exclusive, BecauseExclusive> for T
where T: ?Sized,

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