41

How would I get the browser version being used?

>>> from selenium import webdriver
>>> driver = webdriver.Firefox()
>>> print version <-- how to do this?
    Firefox 12.0
2
  • 1
    From Firefox version 48 and forward you can see the answer here Commented Feb 2, 2018 at 17:21
  • plus to the answer linked by @ArgiesDario driver.capabilities["browserVersion"] Chrome, FF at least both implement that capability now. Commented Oct 20, 2020 at 13:49

9 Answers 9

44

The capabilities property is a dictionary containing information about the browser itself, so this should work:

print(driver.capabilities['version'])
Sign up to request clarification or add additional context in comments.

Comments

42

This answer led me down the right path but is specific to python and the topic is more broad. So, I'm adding an answer for Java which was a bit more tricky. At this time I am using selenium 2.25.0.

//make sure have correct import statements - I had to add these
import org.openqa.selenium.Capabilities;
import org.openqa.selenium.remote.RemoteWebDriver;

WebDriver driver = new FirefoxDriver();

Capabilities caps = ((RemoteWebDriver) driver).getCapabilities();
String browserName = caps.getBrowserName();
String browserVersion = caps.getVersion();
System.out.println(browserName+" "+browserVersion);

Comments

9

If you are using Chrome you can do the following:

driver.capabilities['version']

And if you are using Firefox:

driver.capabilities['browserVersion']

Comments

8

If driver.capabilities['version'] does not work for you, check the capabilities. The version number is there but it might be under a different key. For example I was getting a key error on Windows 10 when trying to access the version number with the version key.

To check capabilities:

print driver.capabilities

For me, this works on Chrome/Linux

driver.capabilities['version']

And this works on Chrome/Windows 10

driver.capabilities['browserVersion']

1 Comment

This worked for me in Mar 2023, Python on macOS, for both Safari and Firefox. I did print(driver.capabilities) to find the keys/values, which informed me to use: driver.capabilities['browserVersion'] to get the version.
3

While this may not quite answer the question above, this still could be useful to someone whose looking for a way to code a test based upon different behaviors they receive from different browsers (i.e. Firefox vs Chrome). I was looking for this at the time when I stumbled upon this thread, so I thought I'd add it in case it can help someone else.

On Python, if you're simply looking for the browser you're testing on (i.e. firefox, chrome, ie, etc..), then you could use...

driver.name

... in an if statement. This assumes you've already assigned driver to the web browser you're testing on (i.e. Firefox, Chrome, IE, etc..). However, if you're tasked with testing multiple versions of the same browser, you'll want something more to driver.version. Hope this helps someone out. I was looking for this solution when I found this thread, so I thought I'd add it just in case someone else needs it.

Comments

2

You can extract the browser version of the GeckoDriver initiated session by accessing the capabilities object which returns a and you can use the following solution:

from selenium import webdriver
from selenium.webdriver.firefox.options import Options

options = Options()
options.binary_location = r'C:\Program Files\Mozilla Firefox\firefox.exe'
driver = webdriver.Firefox(firefox_options=options, executable_path=r'C:\WebDrivers\geckodriver.exe')
my_dict = driver.capabilities
print("Mozilla Firefox browser version is: " + str(my_dict['browserVersion']))
driver.quit()

Console Output:

Mozilla Firefox browser version is: 77.0.1

Extracting all the capabilities

Likewise, you can extract all the properties from the dictionary as follows:

from selenium import webdriver
from selenium.webdriver.firefox.options import Options

options = Options()
options.binary_location = r'C:\Program Files\Mozilla Firefox\firefox.exe'
driver = webdriver.Firefox(firefox_options=options, executable_path=r'C:\WebDrivers\geckodriver.exe')
my_dict = driver.capabilities
for key,val in my_dict.items():
    print (key, "=>", val)
driver.quit()

Console Output:

acceptInsecureCerts => True
browserName => firefox
browserVersion => 77.0.1
moz:accessibilityChecks => False
moz:buildID => 20200602222727
moz:geckodriverVersion => 0.26.0
moz:headless => False
moz:processID => 12668
moz:profile => C:\Users\Soma Bhattacharjee\AppData\Local\Temp\rust_mozprofileFc1B08
moz:shutdownTimeout => 60000
moz:useNonSpecCompliantPointerOrigin => False
moz:webdriverClick => True
pageLoadStrategy => normal
platformName => windows
platformVersion => 10.0
rotatable => False
setWindowRect => True
strictFileInteractability => False
timeouts => {'implicit': 0, 'pageLoad': 300000, 'script': 30000}
unhandledPromptBehavior => dismiss and notify

Comments

1

If your wrapping your WebDriver so that it is EventFiring you'll have to do a custom EventFiringWebDriver implementation.

import org.openqa.selenium.Capabilities;
import org.openqa.selenium.HasCapabilities;
import org.openqa.selenium.remote.RemoteWebDriver;
import org.openqa.selenium.support.events.EventFiringWebDriver;

public class MyEventFiringWebDriver extends EventFiringWebDriver implements HasCapabilities {

    private RemoteWebDriver driver;

    public MyEventFiringWebDriver(RemoteWebDriver driver) {
        super(driver);
        this.driver = driver;
    }

    @Override
    public Capabilities getCapabilities() {
        return driver.getCapabilities();
    }

}

Just posting because it was a problem I ran across.

Comments

1

Just answering this question for Python users who want to print all the capabilities as I was searching for it before I knew it . Below command works.

print driver.capabilities

Comments

1

In C# Selenium I used

ICapabilities capabilities = ((RemoteWebDriver)driver).capabilities;
capabilities.GetCapability("browserName");
capabilities.GetCapability("browserVersion");

It worked fine

1 Comment

The property is now in CamelCase, so .Capabilities. Additionally, you might want to cast to WebDriver, like this ICapabilities capabilities = ((WebDriver)_browser).Capabilities; if you don't use a RemoteWebDriver.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.