Get browser version using selenium webdriver

前端 未结 8 1398
醉话见心
醉话见心 2020-12-06 09:06

How would I get the browser version being used?

>>> from selenium import webdriver
>>> driver = webdriver.Firefox()
>>> print vers         


        
相关标签:
8条回答
  • 2020-12-06 09:30

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

    print(driver.capabilities['version'])
    
    0 讨论(0)
  • 2020-12-06 09:32

    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']
    
    0 讨论(0)
  • 2020-12-06 09:40

    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

    0 讨论(0)
  • 2020-12-06 09: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);
    
    0 讨论(0)
  • 2020-12-06 09:41

    You can extract the browser version of the GeckoDriver initiated firefox session by accessing the capabilities object which returns a dictionary 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
    
    0 讨论(0)
  • 2020-12-06 09:43

    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.

    0 讨论(0)
提交回复
热议问题