Setting Firefox user agent with RemoteDriver and Grid instance

1,181 views
Skip to first unread message

Ian Simpson

unread,
Nov 4, 2011, 4:43:10 PM11/4/11
to Selenium Users
Hey there,

Is it possible to set the user agent when you're dealing with an
instance of RemoteDriver that is using an instance of
DesiredCapabilities.firefox()? I don't see user agent in
CapabilityType at all.

I would think that there would be a way to set up the same
preconditions for Firefox on Grid as their would be when creating a
local instance.

Krishnan Mahadevan

unread,
Nov 4, 2011, 11:42:47 PM11/4/11
to seleniu...@googlegroups.com
You could try and set the user agent as part of the firefox profile, hook up the firefox profile with a DesiredCapability, couple this with a RemotewebDriver instance and get this to work. This is possible because, the firefox profile actually gets relayed to the webdriver node against which the grid executes your test.

Hope that helps !


Thanks & Regards
Krishnan Mahadevan

"All the desirable things in life are either illegal, expensive, fattening or in love with someone else!"



--
You received this message because you are subscribed to the Google Groups "Selenium Users" group.
To post to this group, send email to seleniu...@googlegroups.com.
To unsubscribe from this group, send email to selenium-user...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/selenium-users?hl=en.


Ian Simpson

unread,
Nov 7, 2011, 8:54:44 PM11/7/11
to Selenium Users
Where are you seeing that the profile gets relayed?

In the DesiredCapabilities.firefox(), all I see is:

public static DesiredCapabilities firefox() {
return new DesiredCapabilities("firefox", "", Platform.ANY);
}

which doesn't pass any profile info.

In FirefoxDriver's constructor, I see it calling the super constructor
from RemoteWebDriver, passing the command executor with the profile:

public FirefoxDriver(FirefoxBinary binary, FirefoxProfile profile) {
super(new LazyCommandExecutor(binary, profile),
DesiredCapabilities.firefox());

However, I need to use the RemoteWebDriver constructor of String/
Capabilities to tell it about the Grid instance. Additionally,
LazyCommandExecutor is a private static inner class.

If I'm missing the mark on something here, let me know.

--Ian

On Nov 4, 8:42 pm, Krishnan Mahadevan

Krishnan Mahadevan

unread,
Nov 7, 2011, 9:31:11 PM11/7/11
to seleniu...@googlegroups.com
Ian,
Here's how you get it done :

public static void main(String[] args) throws MalformedURLException {
FirefoxProfile profile = new FirefoxProfile();
DesiredCapabilities dc = DesiredCapabilities.firefox();
dc.setCapability(FirefoxDriver.PROFILE, profile);
URL url = new URL("http://localhost:444/wd/hub");
WebDriver driver = new RemoteWebDriver(url, dc);
}

When the capability is coupled with the firefox profile information, then the firefox profile does get relayed to the actual remote webdriver node against which the Grid executes your test !

Thanks & Regards
Krishnan Mahadevan

"All the desirable things in life are either illegal, expensive, fattening or in love with someone else!"



Ian Simpson

unread,
Nov 8, 2011, 1:32:31 PM11/8/11
to Selenium Users
Ah, some secret sauce happening there I see :)

Thanks a ton for the help. If this isn't documented on the Selenium 2
wiki, it needs to be.

On Nov 7, 7:31 pm, Krishnan Mahadevan

David Grandes

unread,
Nov 23, 2011, 2:34:34 PM11/23/11
to Selenium Users
Hi Krishnan, Ive been trying to accomplish the same thing in Python
but for some reason the server ignores the profile.
This is what Im trying to do:

profile = FirefoxProfile()
profile.set_preference("general.useragent.override", "USER
AGENT STRING")
capabilites = webdriver.DesiredCapabilities.FIREFOX
capabilites["firefox_profile"] =
profile.default_preferences
cls.driver = webdriver.Remote("http://localhost:4444/wd/
hub",capabilites)

This works with grid, but the user agent doesnt load. If run locally,
the user agent does load.

if I change this line:
capabilites["firefox_profile"] = profile.default_preferences
with
capabilites["firefox_profile"] = profile

I get an error that the firefox profile is not json Serializable...


Please Help!

Thanks
David

On Nov 7, 11:31 pm, Krishnan Mahadevan

Krishnan Mahadevan

unread,
Nov 23, 2011, 3:04:12 PM11/23/11
to seleniu...@googlegroups.com
Not sure as to why you would see that in python to be honest David. Never seen that show up in Java bindings.

Thanks & Regards
Krishnan Mahadevan

"All the desirable things in life are either illegal, expensive, fattening or in love with someone else!"


Vaishnavi Desai

unread,
May 8, 2014, 5:20:55 PM5/8/14
to seleniu...@googlegroups.com
Hi,

Thanks for the article. I tried setting the user agent for FF for iPhone, but after I run the program it does not set it for the selenium browser. Used the below code:

FirefoxProfile profile = new FirefoxProfile()
profile.setPreference("general.useragent.overrider", "Mozilla/5.0 (iPhone; U; CPU iPhone OS 3_0 like Mac OS X; en-us) AppleWebKit/528.18 (KHTML, like Gecko) Version/4.0 Mobile/7A341 Safari/528.16")
WEbdriver driver = new FirefoxDriver(profile);

But this does not help!
What am I doing wrong?
Any help is appreciated.

Krishnan Mahadevan

unread,
May 10, 2014, 12:37:00 AM5/10/14
to seleniu...@googlegroups.com
I dont know what can be the problem here. Is the preference correct ?
How are you saying its not getting set ?

Krishnan M
iSent.iPad.
To unsubscribe from this group and stop receiving emails from it, send an email to selenium-user...@googlegroups.com.

To post to this group, send email to seleniu...@googlegroups.com.

Sean Hardaker

unread,
May 24, 2014, 2:08:15 PM5/24/14
to seleniu...@googlegroups.com, ian.g....@gmail.com
Hi

I couldn't find a full answer online but this thread got me going in the right direction. You need to give the webdriver a Firefox profile with your user agent of choice and critically call update_preferences()  and pass that to the desired_capabilities dictionary.  I also included the bit of code if you want to use a proxy but you can just remove that if you don't need it.  It was this but of the documentation that clinched it forme. http://selenium.googlecode.com/git-history/2.34.0/docs/api/py/webdriver_firefox/selenium.webdriver.firefox.firefox_profile.html?highlight=firefox_profile#selenium.webdriver.firefox.firefox_profile.FirefoxProfile.update_preferences 

myProxy = IP:PORT
proxy = Proxy({
        'proxyType': ProxyType.MANUAL,
        'httpProxy': myProxy,
        'ftpProxy': myProxy,
        'sslProxy': myProxy,
        'noProxy': '' # set this value as desired
        })
desired_capabilities = webdriver.DesiredCapabilities.FIREFOX.copy()   
browser_profile = webdriver.FirefoxProfile()          
browser_profile.set_preference("general.useragent.override", "USER AGENT" )           
desired_capabilities["firefox_profile"] = browser_profile.update_preferences() 
driver = webdriver.Remote(   command_executor='IPADDRESS:4444/wd/hub',desired_capabilities=desired_capabilities, browser_profile=browser_profile, proxy = proxy)

Hope that helps someone
Sean
Reply all
Reply to author
Forward
0 new messages