Get browser version using selenium webdriver

49,345

Solution 1

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

print(driver.capabilities['version'])

Solution 2

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);

Solution 3

If you are using Chrome you can do the following:

driver.capabilities['version']

And if you are using Firefox:

driver.capabilities['browserVersion']

Solution 4

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']

Solution 5

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.

Share:
49,345
David542
Author by

David542

Updated on July 09, 2022

Comments

  • David542
    David542 almost 2 years

    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