Peter R
Peter R

Reputation: 85

Selenium WebDriver - Java - Changing the User Agent

i am currently going through selenium webdriver on the selenium wbesite.

There is a section of the website that talks about the "Change user agent" and explains how to do it: https://docs.seleniumhq.org/docs/03_webdriver.jsp#chapter03-reference However, no "real" or "mock" example is given showing this in "action".

So, I was wondering if anyone here, would perhaps have or be able to share a code snippet, with the project included, showing this "in action"? A mock project, you may have or designed yourself to practice this, should suffice.

I have already made a lot of research online, trying to find an example of this, but no success.

Upvotes: 1

Views: 3195

Answers (1)

Fenio
Fenio

Reputation: 3625

From the link you provided, you can change the User-Agent with below code:

Firefox:

FirefoxProfile profile = new FirefoxProfile();
profile.addAdditionalPreference("general.useragent.override", "user-agent-string");
WebDriver driver = new FirefoxDriver(profile);

Chrome:

ChromeOptions chrome = new ChromeOptions();
chrome.addArguments("user-agent=YOUR_USER_AGENT");

It's Java but you can easily switch to any selenium-supported language because all of them implement FirefoxProfile and ChromeOptions

Upvotes: 4

Related Questions