How can I use Selenium with a proxy in Rust?

I have a problem using Selenium (GeckoDriver) with a proxy in Rust.
Here is my code:

use reqwest::{Client, Proxy};
use std::fs::File;
use std::io::Write;
use chrono::Local;
use flate2::read::GzDecoder;
use serde::{Serialize, Deserialize};
use scraper::{Html, Selector};
use serde_json;
use fantoccini::{ClientBuilder, wd::Capabilities};

#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
    let proxy_url = "http://xxx.xxx.xxx:3032";
    let client = Client::builder().proxy(Proxy::all(proxy_url)?).build()?;
    let screenshot_file_name = format!("./snapshot/{}.png", Local::now().format("%Y-%m-%d-%H-%M"));
    take_screenshot(&client, "https://api.ipify.org?format=json", &screenshot_file_name).await?;
    Ok(())
}

async fn take_screenshot(client: &Client, url: &str, file_path: &str) -> Result<(), Box<dyn std::error::Error>> {
    let mut caps = Capabilities::new();
    caps.insert("moz:firefoxOptions".to_string(), serde_json::json!({ "args": ["-headless"] }));
    let web_driver_client = ClientBuilder::native().capabilities(caps).connect("http://localhost:4444").await?;
    web_driver_client.goto(url).await?;
    let png_data = web_driver_client.screenshot().await?;
    std::fs::write(file_path, &png_data)?;
    web_driver_client.close().await?;
    Ok(())
}

I am running Selenium.

xvfb-run geckodriver --port 4444

Why is Selenium not using a proxy?

You aren't using the proxied client for Selenium. You are creating a whole new client for it in web_driver_client.

If you want to use a proxy for the client from fantoccini, use the proxy capability: ClientBuilder in fantoccini - Rust.

1 Like

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.