Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How can I set the user-agent for Chromedriver? #184

Closed
new2rust opened this issue Oct 14, 2023 · 3 comments
Closed

How can I set the user-agent for Chromedriver? #184

new2rust opened this issue Oct 14, 2023 · 3 comments

Comments

@new2rust
Copy link

Hi,

I know for Firefox I there is a method set_user_agent(): https://docs.rs/thirtyfour/latest/src/thirtyfour/common/capabilities/firefox.rs.html#239-241

or for python one can do something like:

from selenium.webdriver.chrome.options import Options
options = Options()
user_agent = "abc"
options.add_argument(f'--user-agent={user_agent}')

But how can it be done with Rust and the Chromedriver option?

Thanks in advance!

@itehax
Copy link

itehax commented Oct 16, 2023

https://docs.rs/thirtyfour/latest/thirtyfour/common/capabilities/chrome/struct.ChromeCapabilities.html
you have this function.
This is an example of usage:

let mut caps = DesiredCapabilities::chrome();
caps.add_chrome_arg("user-agent=Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/102.0.0.0 Safari/537.36").expect("Unable to set user agent");

@matc1294
Copy link

There is a method for this in the docs
So you can do something similar to what you already do in python

let ua = format!("--user-agent={}", get_user_agent_string());
let mut caps = thirtyfour::DesiredCapabilities::chrome();
caps.add_chrome_arg(&ua).expect("Unable to set user_agent");
let browser = thirtyfour::WebDriver::new(&grid_location,caps);

And continue with your usage

@c0d3sling3r
Copy link

If you're trying to set a mobile user-agent, there's another way I used and it worked!

let mut mobile_emulation_hashmap = HashMap::new();
mobile_emulation_hashmap.insert("deviceName", "<DEVICE_NAME>");
caps.add_chrome_option("mobileEmulation", mobile_emulation_hashmap)?;

And you can get device names from chrome device toolbar. Like: "Pixel 7"
image

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants