Skip to main content

WebDriver

Struct WebDriver 

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

The WebDriver struct encapsulates an async Selenium WebDriver browser session.

§Example:

use thirtyfour::prelude::*;

let caps = DesiredCapabilities::firefox();
let driver = WebDriver::new("http://localhost:4444", caps).await?;
driver.goto("https://www.rust-lang.org/").await?;
// Always remember to close the session.
driver.quit().await?;

Implementations§

Source§

impl WebDriver

Source

pub async fn new<S, C>(server_url: S, capabilities: C) -> WebDriverResult<Self>
where S: Into<String>, C: Into<Capabilities>,

Create a new WebDriver as follows:

§Example
let caps = DesiredCapabilities::firefox();
let driver = WebDriver::new("http://localhost:4444", caps).await?;

To customize timeouts, the user agent, the element poller, or supply a custom HttpClient, use WebDriver::builder instead.

§Using Selenium Server
  • For selenium 3.x, you need to also add “/wd/hub” to the end of the url (e.g. “http://localhost:4444/wd/hub”)
  • For selenium 4.x and later, no path should be needed on the url.
§Troubleshooting
  • If the webdriver appears to freeze or give no response, please check that the capabilities’ object is of the correct type for that webdriver.
Source

pub fn builder<S, C>(server_url: S, capabilities: C) -> WebDriverBuilder
where S: Into<String>, C: Into<Capabilities>,

Construct a WebDriverBuilder for advanced session configuration — custom timeouts, user agent, element poller, or a user-supplied HttpClient. Awaiting the builder constructs the session.

For the default configuration, prefer WebDriver::new.

§Example
let caps = DesiredCapabilities::chrome();
let driver = WebDriver::builder("http://localhost:4444", caps)
    .request_timeout(Duration::from_secs(30))
    .user_agent("my-app/1.0")
    .await?;
Source

pub fn handle(&self) -> &Arc<SessionHandle>

Returns a reference to the underlying SessionHandle. Useful for extension code that needs to clone the handle (e.g. building a WebElement from a JSON value via WebElement::from_json). Most users won’t need this — methods on SessionHandle are already reachable through Deref (e.g. driver.session_id()).

Source

pub fn clone_with_config(&self, config: WebDriverConfig) -> Self

Clone this WebDriver keeping the session handle, but supplying a new WebDriverConfig.

This still uses the same underlying client, and still controls the same browser session, but uses a different WebDriverConfig for this instance.

This is useful in cases where you want to specify a custom poller configuration (or some other configuration option) for only one instance of WebDriver.

Source

pub fn managed<C>(capabilities: C) -> WebDriverManagerBuilder
where C: Into<Capabilities>,

Plug-and-play constructor that auto-downloads the appropriate driver (chromedriver / geckodriver), spawns it locally, and tears it down when the last connected WebDriver is dropped.

Returns a WebDriverManagerBuilder pre-loaded with caps. Awaiting the builder constructs the session; chained methods customize the version or other settings before the await:

// Default (matches the locally-installed browser).
let driver = WebDriver::managed(DesiredCapabilities::chrome()).await?;

// Override the version inline using the same builder shape used by
// `WebDriverManager::builder()`.
let driver = WebDriver::managed(caps).latest().await?;

Each call constructs its own WebDriverManager and spawns its own driver subprocess. To share one manager (and its driver subprocesses) across many sessions, construct it explicitly via WebDriverManager::builder and call .launch(caps) on it for each session.

Source

pub fn cdp(&self) -> Cdp

Get a Chrome DevTools Protocol handle for this session.

Cheap to call (just clones the underlying Arc<SessionHandle>). Works on any Chromium-based driver session (chromedriver, msedgedriver, Brave, Opera, etc.) — non-Chromium drivers will return errors when CDP commands are issued.

§Example
let driver = WebDriver::new("http://localhost:4444", DesiredCapabilities::chrome()).await?;
let info = driver.cdp().browser().get_version().await?;
println!("user agent: {}", info.user_agent);

For event subscription, upgrade to [crate::cdp::CdpSession] via Cdp::connect (feature cdp-events).

Source

pub fn driver_id(&self) -> Option<DriverId>

Identifier of the driver process serving this session, if it was launched via WebDriverManager. Returns None for sessions constructed with WebDriver::new (i.e. talking to an externally- managed driver server).

This identifier matches the DriverLogLine::driver_id field on log events emitted via WebDriverManager::on_driver_log, so a closure registered manager-wide can filter to “just this session’s driver”.

Source

pub fn on_driver_log<F>(&self, f: F) -> Option<DriverLogSubscription>
where F: Fn(&DriverLogLine) + Send + Sync + 'static,

Subscribe to log lines from just this session’s driver process. Returns None if this session wasn’t launched via WebDriverManager; otherwise an RAII subscription whose drop unsubscribes.

Lines also continue flowing to any subscribers attached at the manager level (via WebDriverManager::on_driver_log).

Source

pub async fn quit(self) -> WebDriverResult<()>

End the webdriver session and close the browser.

NOTE: Although WebDriver does close when all instances go out of scope. When this happens it blocks the current executor, therefore, if you know when a webdriver is no longer used/required call this method and await it to more or less “asynchronously drop” it this also allows you to catch errors during quitting, and possibly panic or report back to the user

Source

pub fn leak(self) -> Result<(), AlreadyQuit>

Leak the webdriver session and prevent it from being closed, use this if you don’t want your driver to automatically close

Trait Implementations§

Source§

impl Clone for WebDriver

Source§

fn clone(&self) -> WebDriver

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for WebDriver

Source§

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

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

impl Deref for WebDriver

The Deref implementation allows the WebDriver to “fall back” to SessionHandle and exposes all the methods there without requiring us to use an async_trait. See documentation at the top of this module for more details on the design.

Source§

type Target = Arc<SessionHandle>

The resulting type after dereferencing.
Source§

fn deref(&self) -> &Self::Target

Dereferences the 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<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. 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> PolicyExt for T
where T: ?Sized,

Source§

fn and<P, B, E>(self, other: P) -> And<T, P>
where T: 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: 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<P, T> Receiver for P
where P: Deref<Target = T> + ?Sized, T: ?Sized,

Source§

type Target = T

🔬This is a nightly-only experimental API. (arbitrary_self_types)
The target type on which the method may be called.
Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
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<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