### Basic Usage of undetected-chromedriver in Rust Source: https://github.com/ulyssedev/rust-undetected-chromedriver/blob/main/README.md This example demonstrates the fundamental usage of the undetected-chromedriver library in a Rust application. It initializes a Chrome driver instance, navigates to a URL, retrieves the page title, and then quits the driver. This requires the 'tokio' runtime and Chrome to be installed on the system. ```rust use undetected_chromedriver::chrome; use tokio; #[tokio::main] async fn main() -> Result<(), Box> { let driver = chrome().await?; driver.goto("https://www.rust-lang.org/").await?; let title = driver.title().await?; println!("Title: {}", title); driver.quit().await?; Ok(()) } ``` -------------------------------- ### Add undetected-chromedriver Dependency to Cargo.toml Source: https://github.com/ulyssedev/rust-undetected-chromedriver/blob/main/README.md This snippet shows how to add the undetected-chromedriver library as a dependency to your Rust project by modifying the Cargo.toml file. Ensure you have Rust and Cargo installed. ```toml [dependencies] undetected-chromedriver = "0.1.2" ``` -------------------------------- ### Build and Run Docker Container Source: https://context7.com/ulyssedev/rust-undetected-chromedriver/llms.txt These bash commands demonstrate how to build a Docker image from the provided Dockerfile and then run it as a container. The container will execute the Rust binary, leveraging the pre-configured Xvfb environment for headless browser automation. ```bash # Build and run your Docker container docker build -t my-scraper . docker run my-scraper # The xvfb.sh entrypoint automatically manages the virtual display: # - Sets DISPLAY=:1 # - Starts Xvfb with 1280x1024x16 resolution # - Keeps the virtual display running # - Executes your binary ``` -------------------------------- ### Docker Integration for Headless Chrome with Xvfb Source: https://context7.com/ulyssedev/rust-undetected-chromedriver/llms.txt This Dockerfile outlines a multi-stage build process. It first builds a Rust application and then copies the release binary into a pre-built Docker image that includes undetected Chrome and Xvfb. The `xvfb.sh` entrypoint script automatically sets up and manages the virtual display for headless operation. ```dockerfile # Multi-stage build for your Rust application FROM rust:latest as builder COPY ./src ./src COPY ./Cargo.toml ./Cargo.toml COPY ./Cargo.lock ./Cargo.lock RUN cargo build --release # Use the pre-built image with Chrome and Xvfb FROM ghcr.io/ulyssedev/rust-undetected-chromedriver:latest COPY --from=builder /target/release/your_binary /home/apps/your_binary CMD ["/home/apps/your_binary"] ``` -------------------------------- ### Dockerfile for Rust Undetected Chromedriver Application Source: https://github.com/ulyssedev/rust-undetected-chromedriver/blob/main/README.md This Dockerfile outlines the process of building a Rust application that uses undetected-chromedriver and packaging it into a Docker image. It first builds the Rust binary in a builder stage and then copies it into a pre-built image that contains Chrome and xvfb, suitable for headless execution. ```dockerfile FROM rust:latest as builder COPY ./src ./src COPY ./Cargo.toml ./Cargo.toml COPY ./Cargo.lock ./Cargo.lock RUN cargo build --release FROM ghcr.io/ulyssedev/rust-undetected-chromedriver:latest COPY --from=builder /target/release/binary /home/apps/binary CMD ["/home/apps/binary"] ``` -------------------------------- ### Interact with Web Elements using Rust and thirtyfour Source: https://context7.com/ulyssedev/rust-undetected-chromedriver/llms.txt This Rust code snippet demonstrates how to use the undetected_chromedriver and thirtyfour libraries to navigate to a webpage, query for specific elements using XPath, wait for them to become clickable or displayed, click them, and extract text content. It requires the `tokio` runtime and `undetected_chromedriver` with `thirtyfour` dependencies. ```rust use undetected_chromedriver::chrome; use thirtyfour::prelude::{ElementQueryable, ElementWaitable}; use thirtyfour::By; #[tokio::main] async fn main() -> Result<(), Box> { let driver = chrome().await?; driver.goto("https://recaptcha-demo.appspot.com/recaptcha-v3-request-scores.php").await?; // Query for a button element using XPath let button = driver .query(By::XPath(r"//*[@id=\"recaptcha-steps\"]/li[2]/button[2]")) .first() .await?; // Wait until the element is clickable button.wait_until().clickable().await?; // Click the button button.click().await?; // Wait for response element to be displayed let response = driver .query(By::XPath(r"//*[@id=\"recaptcha-steps\"]/li[5]/pre")) .first() .await?; response.wait_until().displayed().await?; // Extract text content let response_text = response.text().await?; println!("Response: {}", response_text); driver.quit().await?; Ok(()) } ``` -------------------------------- ### Navigate with Tab Management using Chrome::goto() Source: https://context7.com/ulyssedev/rust-undetected-chromedriver/llms.txt An enhanced navigation method that opens URLs in new tabs and manages window handles to mimic human behavior and avoid detection. This method is part of the Chrome trait for managing browser navigation. ```rust use undetected_chromedriver::Chrome; use thirtyfour::WebDriver; #[tokio::main] async fn main() -> Result<(), Box> { let driver: WebDriver = Chrome::new().await; // Navigate using the Chrome trait's goto method // This opens URL in a new tab, closes the original tab, // and switches to the new tab - mimicking human behavior driver.goto("https://example.com").await?; // Page is now loaded in the active tab let source = driver.source().await?; println!("Page loaded, source length: {} bytes", source.len()); driver.quit().await?; Ok(()) } ``` -------------------------------- ### Cargo.toml Dependencies for Rust Automation Source: https://context7.com/ulyssedev/rust-undetected-chromedriver/llms.txt This TOML snippet shows the necessary dependencies to include in your `Cargo.toml` file for using Rust Undetected ChromeDriver. It specifies the versions for `undetected-chromedriver`, `tokio` (with the `full` feature enabled for async runtime), and `thirtyfour`. ```toml [package] name = "my-automation-project" version = "0.1.0" edition = "2021" [dependencies] undetected-chromedriver = "0.1.2" tokio = { version = "1.28", features = ["full"] } thirtyfour = "0.31.0" ``` -------------------------------- ### Create WebDriver Instance using Chrome::new() Trait Source: https://context7.com/ulyssedev/rust-undetected-chromedriver/llms.txt An alternative method for creating an undetected WebDriver instance using the Chrome trait. This provides an object-oriented approach to WebDriver creation, suitable for scenarios where a trait-based interface is preferred. ```rust use undetected_chromedriver::Chrome; use thirtyfour::WebDriver; #[tokio::main] async fn main() -> Result<(), Box> { // Create WebDriver using the Chrome trait let driver: WebDriver = Chrome::new().await; // Use the driver for automation let title = driver.title().await?; println!("Browser initialized: {}", title); driver.quit().await?; Ok(()) } ``` -------------------------------- ### Create Undetected WebDriver Instance with chrome() Source: https://context7.com/ulyssedev/rust-undetected-chromedriver/llms.txt The primary function to create an undetected WebDriver instance. It automatically detects the Chrome version, downloads and patches ChromeDriver, and configures the browser for anti-detection. This is the main entry point for most use cases. ```rust use undetected_chromedriver::chrome; use tokio; #[tokio::main] async fn main() -> Result<(), Box> { // Create a new undetected Chrome instance // This automatically: // - Detects your installed Chrome version // - Downloads matching ChromeDriver if not present // - Patches ChromeDriver to remove detection markers // - Configures browser with anti-detection settings let driver = chrome().await?; // Navigate to a website driver.goto("https://www.rust-lang.org/").await?; // Get the page title let title = driver.title().await?; println!("Title: {}", title); // Always quit the driver when done driver.quit().await?; Ok(()) } // Output: Title: Rust Programming Language ``` -------------------------------- ### Bypass Cloudflare Challenges with Chrome::bypass_cloudflare() Source: https://context7.com/ulyssedev/rust-undetected-chromedriver/llms.txt Automates the process of solving Cloudflare's browser verification challenges. It locates and clicks the challenge button within the iframe, facilitating access to Cloudflare-protected websites. ```rust use undetected_chromedriver::Chrome; use thirtyfour::prelude::ElementQueryable; use thirtyfour::{By, WebDriver}; #[tokio::main] async fn main() -> Result<(), Box> { let driver: WebDriver = Chrome::new().await; // Navigate to a Cloudflare-protected site and bypass the challenge driver.bypass_cloudflare("https://nowsecure.nl").await?; // Wait for the page to fully load after bypass tokio::time::sleep(std::time::Duration::from_secs(2)).await; // Verify bypass was successful let result = driver.query(By::XPath("/html/body/div[2]/div/main/p[1]")) .first() .await?; let text = result.text().await?; println!("Bypass result: {}", text); // Expected output: "you passed!" driver.quit().await?; Ok(()) } ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.