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
impl WebDriver
Sourcepub async fn new<S, C>(server_url: S, capabilities: C) -> WebDriverResult<Self>
pub async fn new<S, C>(server_url: S, capabilities: C) -> WebDriverResult<Self>
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.
Sourcepub fn builder<S, C>(server_url: S, capabilities: C) -> WebDriverBuilder
pub fn builder<S, C>(server_url: S, capabilities: C) -> WebDriverBuilder
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?;Sourcepub fn handle(&self) -> &Arc<SessionHandle> ⓘ
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()).
Sourcepub fn clone_with_config(&self, config: WebDriverConfig) -> Self
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.
Sourcepub fn managed<C>(capabilities: C) -> WebDriverManagerBuilderwhere
C: Into<Capabilities>,
pub fn managed<C>(capabilities: C) -> WebDriverManagerBuilderwhere
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.
Sourcepub fn cdp(&self) -> Cdp
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).
Sourcepub fn driver_id(&self) -> Option<DriverId>
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”.
Sourcepub fn on_driver_log<F>(&self, f: F) -> Option<DriverLogSubscription>
pub fn on_driver_log<F>(&self, f: F) -> Option<DriverLogSubscription>
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).
Sourcepub async fn quit(self) -> WebDriverResult<()>
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