Yuki
Yuki

Reputation: 23

How to set a proxy with authentication when using the headless_chrome crate?

let proxy = format!("https://{}:{}@{}:{}", "username", "password", "proxy_host", "proxy_port");
let browser = Browser::new(LaunchOptions {
    headless: true,
    proxy_server: Some(&proxy),
    ..Default::default()
}).expect("Failed to launch browser");

This is my code to launch a headless browser using the headless_chrome crate. When I connect to site like the below, I get the error: Navigate failed: net::ERR_NO_SUPPORTED_PROXIES.

let tab = browser.new_tab().unwrap();

// Navigate to wikipedia
tab.navigate_to("https://www.semrush.com/login/?src=header&redirect_to=%2F").expect("Error");

How can I fix this problem?

Upvotes: 1

Views: 41

Answers (1)

Yuki
Yuki

Reputation: 23

let proxy = format!("http://{}:{}", "proxy_host", "proxy_port");
let launch_options = LaunchOptions::default_builder()
    .proxy_server(Some(&proxy))
    .build()
    .unwrap();
let browser = Browser::new(launch_options).expect("Failed to launch browser");

let tab = browser.new_tab().unwrap();
tab.wait_until_navigated().unwrap();

tab.enable_fetch(None, Some(true)).unwrap()
    .authenticate(Some("username".to_string()), Some("password".to_string())).unwrap()
    .navigate_to("https://example.com").unwrap()
    .wait_until_navigated().unwrap();

This is my solution. When using headless_chrome crate, we can't set the proxy with authentication directly like http://username:password@host:port format. Instead, we have authenticate function that enables to set the username and password on tab.

This link will be help. https://github.com/rust-headless-chrome/rust-headless-chrome/issues/417

Upvotes: 0

Related Questions