How to get Browserversion and browser name?

13,854 views
Skip to first unread message

jyo rani

unread,
Feb 7, 2012, 9:08:05 PM2/7/12
to Selenium Users
This seems to be so hard for me.I am a qtp user moving towards
selenium2.


How do i get browsername and browserversion with Webdriver?

I need to achieve it with IE,Chrome and FF browsers.


Please help.

Thanks

Krishnan Mahadevan

unread,
Feb 7, 2012, 9:23:48 PM2/7/12
to seleniu...@googlegroups.com
Driver.executeScript("return navigator.userAgent","")
Parse the string that you would get from this and you should be able to get the browser name and version.
> --
> 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.
>
>

--
Thanks & Regards
Krishnan Mahadevan

"All the desirable things in life are either illegal, expensive, fattening or in love with someone else!"
My Scribblings @ http://wakened-cognition.blogspot.com/

jyo rani

unread,
Feb 8, 2012, 12:18:11 AM2/8/12
to seleniu...@googlegroups.com
i tried the  Driver.executeScript("return navigator.userAgent","")
For IE it doesn't return any useragent. so not sure how to get useragent.

Can you tell me how to parse the string?

Sorry for basic questions...just started learning selenium....

Krishnan Mahadevan

unread,
Feb 8, 2012, 12:43:40 AM2/8/12
to seleniu...@googlegroups.com
Were you doing something like this for IE ?

WebDriver driver = new InternetExplorerDriver();
String userAgent = driver.executeScript("return navigator.userAgent","");

Once you have the userAgent value in the string, its only a question of you parsing it using methods available in the String class.


Thanks & Regards
Krishnan Mahadevan

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


skonthebass

unread,
Sep 24, 2014, 8:58:39 AM9/24/14
to seleniu...@googlegroups.com
For posterity sake, driver.GetType().Name.ToString()  (this is C# implementation)

Kevin O.

unread,
Sep 24, 2014, 9:24:25 AM9/24/14
to seleniu...@googlegroups.com
The OP wanted version too. In Python this you can inspect the actual capabilities by looking at driver.capabilities (a dictionary)
In C#, I would think it would look more like
String name = ((RemoteWebDriver) driver).Capabilities.BrowserName
String version = ((RemoteWebDriver) driver).Capabilities.Version

Palla Subramanyam

unread,
Apr 20, 2015, 7:58:55 AM4/20/15
to seleniu...@googlegroups.com
Hi Uttam,
i tried below script but in console displayed null as browser name nothing else.

On Wednesday, September 24, 2014 at 7:20:49 PM UTC+5:30, Uttam karimbedu wrote:

Gaurav Chaturvedi

unread,
Apr 20, 2015, 11:28:11 AM4/20/15
to seleniu...@googlegroups.com
Hi Palla,

Please try with the below snippet of code.
Code:
 driver.get("****URL******");
        String s = (String) ((JavascriptExecutor) driver).executeScript("return navigator.userAgent;");
        System.out.println("Browser name : " + s);


Regards,
Gaurav Chaturvedi

--
You received this message because you are subscribed to the Google Groups "Selenium Users" group.
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.

Krishnan Mahadevan

unread,
Apr 20, 2015, 12:01:12 PM4/20/15
to Selenium Users
Couple the output of the Javascript that Gaurav shared with a code snippet as below 


and you should have a readable output.

Ofcourse you would need to have a maven dependency as below :





Thanks & Regards
Krishnan Mahadevan

"All the desirable things in life are either illegal, expensive, fattening or in love with someone else!"
My Scribblings @ http://wakened-cognition.blogspot.com/
My Technical Scribbings @ http://rationaleemotions.wordpress.com/

Oscar Rieken

unread,
Apr 20, 2015, 12:32:39 PM4/20/15
to seleniu...@googlegroups.com
Late to chime in but...

Don't you know the versions because you are launching them from your code?
and you have them installed on the machine you are running the tests on?




Krishnan Mahadevan

unread,
Apr 20, 2015, 11:06:40 PM4/20/15
to Selenium Users
Oscar,

Here's a typical scenario wherein this would be useful for sure.

Lets say your capabilities read as just "Firefox on MAC" and lets say you are leveraging a cloud execution environment such as SauceLabs. In those cases your test is going to be routed to a firefox browser on MAC but what version of MAC and what version of Firefox it was executed against is something that you wouldn't know.

Another use case is when you have a bunch of windows nodes each of which is perhaps running a different version of firefox version in your in house grid and you would like to know which browser version it was executed against.

So there are use cases wherein this is a need of the hour. 

Chrome browser is another use case [ it keeps auto upgrading itself and the mechanism of turning off the auto upgrade doesnt seem to be an easy thing ] so it would be good to find out what is the browser version in this case as well.


Thanks & Regards
Krishnan Mahadevan

"All the desirable things in life are either illegal, expensive, fattening or in love with someone else!"
My Scribblings @ http://wakened-cognition.blogspot.com/
My Technical Scribbings @ http://rationaleemotions.wordpress.com/

Oscar Rieken

unread,
Apr 20, 2015, 11:23:02 PM4/20/15
to seleniu...@googlegroups.com
On Mon, Apr 20, 2015 at 11:06 PM, Krishnan Mahadevan <krishnan.ma...@gmail.com> wrote:
Oscar,

Here's a typical scenario wherein this would be useful for sure.

Lets say your capabilities read as just "Firefox on MAC" and lets say you are leveraging a cloud execution environment such as SauceLabs. In those cases your test is going to be routed to a firefox browser on MAC but what version of MAC and what version of Firefox it was executed against is something that you wouldn't know.
I never actually used Saucelabs. but this makes sense. 

 
Another use case is when you have a bunch of windows nodes each of which is perhaps running a different version of firefox version in your in house grid and you would like to know which browser version it was executed against.
wouldn't you just specify the version and platform in the capabilities then pass that to the browser when you create the instance of it? 
Thats how I do it, but then make those all env variables that can be passed in when I execute the tests themselves.  

 
 
So there are use cases wherein this is a need of the hour. 

 
Chrome browser is another use case [ it keeps auto upgrading itself and the mechanism of turning off the auto upgrade doesnt seem to be an easy thing ] so it would be good to find out what is the browser version in this case as well.

this on windows is easier i just use the chocolatey package for the specific version I need then connect that to my grid 
 

tulsi.tester

unread,
Apr 21, 2015, 11:54:37 PM4/21/15
to seleniu...@googlegroups.com
Hi,

Use the following code snippet.


System.setProperty("webdriver.chrome.driver","path to your chrome driver.exe");
WebDriver driver = new ChromeDriver();
Capabilities caps = ((RemoteWebDriver)driver).getCapabilities();
System.out.println(caps.getBrowserName());
System.out.println(caps.getVersion());
System.out.println(caps.getPlatform());

//we can also find using user agent. After getting string we have to parse it
JavascriptExecutor js = (JavascriptExecutor)driver;
String useragent = (String)js.executeScript("return navigator.userAgent;");
System.out.println("user agent is "+useragent);

CH!NN@ K

unread,
Sep 24, 2014, 9:50:49 AM9/24/14
to seleniu...@googlegroups.com

deepthi cynthia

unread,
Jul 17, 2016, 1:08:47 AM7/17/16
to Selenium Users
If you are using protractor you can get the browser's info 

browser.getCapabilities().then(function(c) {

                 console.log(c);

                console.log(c.get('browserName'));

});

Purushotham Karimbedu

unread,
Jul 18, 2016, 6:22:54 AM7/18/16
to seleniu...@googlegroups.com

---
Thanks & Regards,
Purushotham Karimbedu,
Drupal Developer and QA Engineer,
Phone no: 09000109120.


--
You received this message because you are subscribed to the Google Groups "Selenium Users" group.
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.

Always a student

unread,
Sep 27, 2017, 10:58:42 AM9/27/17
to Selenium Users
Were you able to achieve it?

I am on C# and tried both below approaches and didnt get what I wanted.

a) string BrowserVersion = ((RemoteWebDriver)DriverContext.Driver).Capabilities.Version;
     This one worked with Chrome but FF and IE are returning an empty string.
b) string userAgent = driver.ExecuteScript("return navigator.userAgent","");
    I tried this with IE and it returned this weird string that seems to imply it is Mozilla
    "Mozilla/5.0 (Windows NT 10.0; WOW64; Trident/7.0; .NET4.0C; .NET4.0E; .NET CLR 2.0.50727; .NET CLR 3.0.30729; .NET CLR 3.5.30729; rv:11.0) like Gecko"

Any other suggestion anyone (for c#)?

On Tuesday, February 7, 2012 at 9:08:05 PM UTC-5, testingzeal wrote:

Krishnan Mahadevan

unread,
Sep 27, 2017, 12:06:38 PM9/27/17
to seleniu...@googlegroups.com

Now that you have the user agent string, you might want to use a library such as this : https://github.com/ua-parser/uap-csharp

 

To parse the user agent and extract values out of it.

 

Thanks & Regards

Krishnan Mahadevan

 

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

My Scribblings @ http://wakened-cognition.blogspot.com/

My Technical Scribbings @ http://rationaleemotions.wordpress.com/

 

--

You received this message because you are subscribed to the Google Groups "Selenium Users" group.
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.

Always a student

unread,
Sep 28, 2017, 4:28:07 PM9/28/17
to Selenium Users
Thanks :)

My issue here is, it is telling me it is mozilla firefox where as it is actually IE that I executed the test on.

"   I tried this with IE and it returned this weird string that seems to imply it is Mozilla
    "Mozilla/5.0 (Windows NT 10.0; WOW64; Trident/7.0; .NET4.0C; .NET4.0E; .NET CLR 2.0.50727; .NET CLR 3.0.30729; .NET CLR 3.5.30729; rv:11.0) like Gecko""

So, i think even before parsing, I havent gotteen to a point where it is returning me the correct info.

Krishnan Mahadevan

unread,
Sep 29, 2017, 2:41:36 AM9/29/17
to seleniu...@googlegroups.com

The user agent you shared is not firefox. It is IE

 

Open up the URL : https://developers.whatismybrowser.com/useragents/parse/

 

And paste the user agent:

 

Mozilla/5.0 (Windows NT 10.0; WOW64; Trident/7.0; .NET4.0C; .NET4.0E; .NET CLR 2.0.50727; .NET CLR 3.0.30729; .NET CLR 3.5.30729; rv:11.0) like Gecko

 

And parse it to see the results for yourself.

Always a student

unread,
Sep 29, 2017, 10:09:56 AM9/29/17
to Selenium Users
Oh Okay. Thanks very very much Krishnan :)

Always a student

unread,
Sep 29, 2017, 5:48:28 PM9/29/17
to Selenium Users
It works as expected but i noticed the NuGet package is more than 300mb. Even my whole solution combined is less than 325mb and just this one package is >300mb. Its adding a lot of stuff  like below. Is there any alternative to get this UAParser?

Pravesh Jha

unread,
Nov 22, 2017, 11:09:18 PM11/22/17
to Selenium Users
U dnt need to get user agent lets write simple code 
 C# and selenium

 ICapabilities capabilities = ((RemoteWebDriver)driver).Capabilities;
                strBrowserVersion = capabilities.Version.ToString();
                if (strBrowserVersion == "")
                    strBrowserVersion = capabilities.GetCapability("browserVersion").ToString();
Reply all
Reply to author
Forward
0 new messages