12 releases
new 0.1.11 | Jan 9, 2025 |
---|---|
0.1.10 | Jan 5, 2025 |
0.1.5 | Dec 29, 2024 |
#19 in WebSocket
1,242 downloads per month
Used in 2 crates
210KB
5K
SLoC
webdriverbidi
Overview
The webdriverbidi library provides an interface for interacting with web browsers through the WebDriver BiDi (Bidirectional) protocol. This library allows you to create and manage WebDriver sessions, send commands, and handle responses asynchronously through WebSockets.
Features
- Create and manage WebDriver BiDi sessions
- Send commands
- Handle events asynchronously
Getting Started
Prerequisites
- Rust and Cargo installed
- A WebDriver server that supports the BiDi protocol
Installation
Add the following to your Cargo.toml
:
[dependencies]
webdriverbidi = "0.1.11"
Usage
Start a WebDriver BiDi compliant server
$ geckodriver --host=localhost --port=4444
Create a new Rust project and add the following code to src/main.rs
:
use tokio;
use tokio::time;
// --------------------------------------------------
use webdriverbidi::remote::browsing_context::{
GetTreeParameters, NavigateParameters, ReadinessState,
};
use webdriverbidi::session::WebDriverBiDiSession;
use webdriverbidi::webdriver::capabilities::{Capabilities, CapabilityRequest};
// --------------------------------------------------
async fn sleep(secs: u64) {
time::sleep(time::Duration::from_secs(secs)).await
}
// --------------------------------------------------
#[tokio::main]
async fn main() {
// Define the capabilities for the WebDriver session
let always_match = CapabilityRequest::new();
let capabilities = Capabilities::new(always_match);
// Initialize a new WebDriver BiDi session and start it
let host = String::from("localhost");
let port = 4444;
let mut bidi_session = WebDriverBiDiSession::new(host, port, capabilities);
let _ = bidi_session.start().await.expect("Failed to start session");
// Get the browsing context tree
let get_tree_params = GetTreeParameters::new(None, None);
let get_tree_rslt = bidi_session
.browsing_context_get_tree(get_tree_params)
.await
.expect("Failed to send command");
// Navigate to rust-lang.org
let navigate_params = NavigateParameters::new(
get_tree_rslt.contexts[0].context.clone(),
"https://www.rust-lang.org/".to_string(),
Some(ReadinessState::Complete),
);
let _ = bidi_session
.browsing_context_navigate(navigate_params)
.await
.expect("Failed to send command");
sleep(2).await;
// Close the session
bidi_session.close().await.expect("Failed to close session");
}
Module Coverage
session
Types
- session.CapabilitiesRequest
- session.CapabilityRequest
- session.ProxyConfiguration
- session.UserPromptHandler
- session.UserPromptHandlerType
- session.Subscription
- session.SubscriptionRequest
- session.UnsubscribeByIDRequest
- session.UnsubscribeByAttributesRequest
Commands
- session.status
- session.new
- session.end
- session.subscribe
- session.unsubscribe
browser
Types
- browser.ClientWindow
- browser.ClientWindowInfo
- browser.UserContext
- browser.UserContextInfo
Commands
- browser.close
- browser.createUserContext
- browser.getClientWindows
- browser.getUserContexts
- browser.removeUserContext
- browser.setClientWindowState
browsingContext
Types
- browsingContext.BrowsingContext
- browsingContext.Info
- browsingContext.Locator
- browsingContext.Navigation
- browsingContext.NavigationInfo
- browsingContext.ReadinessState
- browsingContext.UserPromptType
Commands
- browsingContext.activate
- browsingContext.captureScreenshot
- browsingContext.close
- browsingContext.create
- browsingContext.getTree
- browsingContext.handleUserPrompt
- browsingContext.locateNodes
- browsingContext.navigate
- browsingContext.print
- browsingContext.reload
- browsingContext.setViewport
- browsingContext.traverseHistory
Events
- browsingContext.contextCreated
- browsingContext.contextDestroyed
- browsingContext.navigationStarted
- browsingContext.fragmentNavigated
- browsingContext.historyUpdated
- browsingContext.domContentLoaded
- browsingContext.load
- browsingContext.downloadWillBegin
- browsingContext.navigationAborted
- browsingContext.navigationFailed
- browsingContext.userPromptClosed
- browsingContext.userPromptOpened
network
Types
- network.AuthChallenge
- network.AuthCredentials
- network.BaseParameters
- network.BytesValue
- network.Cookie
- network.CookieHeader
- network.FetchTimingInfo
- network.Header
- network.Initiator
- network.Intercept
- network.Request
- network.RequestData
- network.ResponseContent
- network.ResponseData
- network.SetCookieHeader
- network.UrlPattern
Commands
- network.addIntercept
- network.continueRequest
- network.continueResponse
- network.continueWithAuth
- network.failRequest
- network.provideResponse
- network.removeIntercept
- network.setCacheBehavior
Events
- network.authRequired
- network.beforeRequestSent
- network.fetchError
- network.responseCompleted
- network.responseStarted
script
Types
- script.Channel
- script.ChannelValue
- script.EvaluateResult
- script.ExceptionDetails
- script.Handle
- script.InternalId
- script.LocalValue
- script.PreloadScript
- script.Realm
- script.PrimitiveProtocolValue
- script.RealmInfo
- script.RealmType
- script.RemoteReference
- script.RemoteValue
- script.ResultOwnership
- script.SerializationOptions
- script.SharedId
- script.StackFrame
- script.StackTrace
- script.Source
- script.Target
Commands
- script.addPreloadScript
- script.disown
- script.callFunction
- script.evaluate
- script.getRealms
- script.removePreloadScript
Events
- script.message
- script.realmCreated
- script.realmDestroyed
storage
Types
- storage.PartitionKey
Commands
- storage.getCookies
- storage.setCookie
- storage.deleteCookies
log
Types
- log.LogEntry
Events
- log.entryAdded
input
Types
- input.ElementOrigin
Commands
- input.performActions
- input.releaseActions
- input.setFiles
webExtension
Types
- webExtension.Extension
Commands
- webExtension.install
- webExtension.uninstall
Contributing
Contributions are welcome! Please open an issue or submit a pull request.
License
This project is licensed under the MIT License. See the LICENSE file for details.
Dependencies
~7–19MB
~253K SLoC