26

I'm running a application using selenium, and I want to know actual chrome browser version installed, before running Chrome Driver to avoid any Exception for compatibility reason. I know I can use driver = webdriver.Chrome("path\\to\\chromedriver.exe") then driver.capabilities['browserVersion'] to show version but if Chrome Driver version differ from actual chrome browser version that's raise an exception.

Thanks

Edited: Actually I found the answer for myself, the solution I found:

from win32com.client import Dispatch

def get_version_via_com(filename):
    parser = Dispatch("Scripting.FileSystemObject")
    try:
        version = parser.GetFileVersion(filename)
    except Exception:
        return None
    return version

if __name__ == "__main__":
    paths = [r"C:\Program Files\Google\Chrome\Application\chrome.exe",
             r"C:\Program Files (x86)\Google\Chrome\Application\chrome.exe"]
    version = list(filter(None, [get_version_via_com(p) for p in paths]))[0]
    print(version)
    # result: 80.0.3987.122

PS: I think people don't understand my question at the beginning and I'm sorry for my english

temascal
  • 28
  • 4
leminhnguyen
  • 1,518
  • 2
  • 13
  • 19
  • Did you find a solution? If yes, it would be much appreciated it if you share it. Thanks. – mikey Feb 26 '20 at 07:32
  • 2
    @mikey Hi guy, Thank you for understanding my question, the solution was posted you can check now and ask me if there is something need to be clear. – leminhnguyen Feb 28 '20 at 03:08
  • Thanks for posting the solution, saved me some time :). In fact, you just need Dispatch: win32com.client.Dispatch.Dispatch("Scripting.FileSystemObject").GetFileVersion(paths[1]), resulted in '80.0.3987.116' in my Win10 box. – mikey Feb 28 '20 at 04:49
  • @mikey Thanks for correcting me, I have remove `netifaces` module, because I played around with many tests in one file so I forgot to removed redundant module – leminhnguyen Feb 28 '20 at 07:25
  • 1
    I've added the solution, since I couldn't provide an answer due to thread being closed. – howdoicode Aug 28 '20 at 16:07
  • 2
    Get this package, this will help you out: [link](https://pypi.org/project/chromedriver-autoinstaller/) , then call: `gc_version = chromedriver_autoinstaller.get_chrome_version() print(gc_version)` – Atish Paul Apr 17 '22 at 14:54

1 Answers1

3

If you are using selenium, then you can get the chrome browser version using the driver.capabilities dictionary.

driver.capabilities['browserVersion']

Earlier version of chromedriver stored the chrome browser version driver.capabilities['version']. If you want to get chrome browser version without having to worry about this, you can use the below code.

if 'browserVersion' in driver.capabilities:
    print(driver.capabilities['browserVersion'])
else:
    print(driver.capabilities['version'])
CodeIt
  • 3,492
  • 3
  • 26
  • 37
  • 2
    I know that, but when using selenium: `driver = webdriver.Chrome("path\\to\\chromedriver.exe")` the program can be crashed before running `driver.capabilities['version']` (reason is chrome version not compatilble, so I must check chrome version first) – leminhnguyen Aug 10 '19 at 10:52
  • 1
    @leminhnguyenHUST Are you looking for a method to get current chrome browser version without loading the `webdriver` ? – CodeIt Aug 10 '19 at 10:58
  • Ohh right, I think we can use Python's library likes `request`, is it feasiable ? (my own opinion) – leminhnguyen Aug 10 '19 at 11:04
  • @leminhnguyenHUST That really depends upon your requirement. For this question, i think my answer is sufficient. – CodeIt Aug 10 '19 at 11:08