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?
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.
The usual way to change the user agent for Firefox is to set the variable "general. useragent. override" in your Firefox profile.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With