Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

WebDriver FireFoxProfile UserAgent switching with FireFoxDriver

I'm wondering if I can change the user agent profile on the fly, without creating a new instance of the ForeFoxDriver? I have the following code, which I can pass in the user agent for iphone or ipad, etc.. it works fine, but i'm having to create a new instance for each test which is slow as it opens/closes the browser. E.g. After doing my tests with iPhone user agent, I'd like to switch the profile to Android user agent or iPad user agent, does the driver get the option to change this without needing to create a new instance?

FirefoxProfile profile = new FirefoxProfile();
profile.setPreference("general.useragent.override","some user agent, like iphone or iPad");
WebDriver driver = new FirefoxDriver(profile);

// do some tests
// set profile to new user agent
// profile.setPreference("general.useragent.override","some android user agent");

// can i now set the driver to the new profile here and perform more tests on this same instance of the driver?
like image 896
Green Avatar asked Feb 01 '12 23:02

Green


People also ask

How do I change userAgent in selenium?

To change the user Agent, we shall take the help of ChromeOptions class. Then apply the add_argument method on the object created. We shall pass user-agent and <value of the user Agent> as parameters to that method.

Which selenium code is used to dynamically change the user agent loaded in the Firefox?

The usual way to change the user agent for Firefox is to set the variable "general. useragent. override" in your Firefox profile.

How do I find user agent in selenium?

Selenium executes JavaScript commands with the help of the execute_script method. To obtain the user Agent information, we have to pass the return navigator. userAgent parameter to the execute_script method. Selenium does have a direct method the to get or modify user Agent.


1 Answers

RemoteWebdriver (parent class of FirefoxDriver) updates the capabilities map only when the session is starting (in the constructor). Unfortunately we can't modify it elsewhere. I think it is a very good idea.

like image 85
Pazonec Avatar answered Nov 11 '22 15:11

Pazonec