Get browser version using selenium webdriver

Question:

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
Asked By: David542

||

Answers:

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

print(driver.capabilities['version'])
Answered By: Matthew Trevor

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);
Answered By: Lukus

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.

Answered By: Reverse Tarzan

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.

Answered By: rwbyrd

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

Answered By: Kireeti Annamaraj

If you are using Chrome you can do the following:

driver.capabilities['version']

And if you are using Firefox:

driver.capabilities['browserVersion']
Answered By: Illia Abielientsev

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']
Answered By: Artur Spirin

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 FilesMozilla Firefoxfirefox.exe'
driver = webdriver.Firefox(firefox_options=options, executable_path=r'C:WebDriversgeckodriver.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 FilesMozilla Firefoxfirefox.exe'
driver = webdriver.Firefox(firefox_options=options, executable_path=r'C:WebDriversgeckodriver.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:UsersSoma BhattacharjeeAppDataLocalTemprust_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
Answered By: undetected Selenium

In C# Selenium I used

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

It worked fine

Answered By: Nitin kumar