Get browser version using selenium webdriver
Asked Answered
C

9

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
Checkered answered 23/9, 2012 at 20:54 Comment(2)
From Firefox version 48 and forward you can see the answer hereBioluminescence
plus to the answer linked by @Bioluminescence driver.capabilities["browserVersion"] Chrome, FF at least both implement that capability now.Shortly
K
44

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

print(driver.capabilities['version'])
Katydid answered 24/9, 2012 at 5:47 Comment(0)
A
41

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);
Amphimixis answered 28/11, 2012 at 18:30 Comment(0)
W
8

If you are using Chrome you can do the following:

driver.capabilities['version']

And if you are using Firefox:

driver.capabilities['browserVersion']
Wileywilfong answered 23/8, 2018 at 12:41 Comment(0)
M
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']
Morena answered 12/8, 2019 at 19:9 Comment(1)
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.Southsoutheast
N
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.

Niel answered 12/2, 2015 at 22:22 Comment(0)
P
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
Prostomium answered 9/7, 2020 at 17:59 Comment(0)
S
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.

Spaniard answered 23/8, 2013 at 3:6 Comment(0)
M
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

Marko answered 26/7, 2018 at 13:58 Comment(0)
B
1

In C# Selenium I used

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

It worked fine

Bitternut answered 25/10, 2022 at 8:18 Comment(1)
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.Galata

© 2022 - 2024 — McMap. All rights reserved.