Find Outdated ChromeDriver Version Without Opening Executable (Python)

Viewed 542

Essentially, I'm trying to find the ChromeDriver version without actually opening it. The reason for this is that I want to auto update the driver. I've sorted the code out for updating it.

The issue is that when I intentionally download an unsupported version to check my code works, it says "This driver only supports Chrome Version 86 and you're on 88.001.xyz" etc.

I was wondering if there was any way of reading the 86 from the ChromeDriver executable so that I can recognise it's not equal to 88 (my chrome browser version)? By doing this, it'll trigger a procedure to go and download the correct chromedriver.

I've attached my code for checking the chromedriver version. I've tried headless but I'm sure it doesn't work.

def get_chrome_version():
    global browser_version_number
    options = Options()
    options.headless = True
    browser_version_driver = webdriver.Chrome("chromedriver_win32/chromedriver.exe", chrome_options=options)
    # browser_version_driver = webdriver.Chrome("chromedriver_win32/chromedriver.exe")
    # browser_version_driver.set_window_position(-10000,0)
    browser_version_number = (browser_version_driver.capabilities['browserVersion'])
    browser_version_number = browser_version_number.split(".")[0]
    chromedriverversion = browser_version_driver.capabilities['chrome']['chromedriverVersion'].split('.')[0]
    print(browser_version_number)
    print(chromedriverversion)



    if browser_version_number != chromedriverversion:
        update_chrome_version()
    
    browser_version_driver.quit()
2 Answers

you could have your python script make a shell call to execute the command ./chromedriver --version (using the relevant path), then parse the result to find your current installed version.

Alrighty, so I found the solution. It all lied within the exception that was outputted in the terminal. Basically my thinking was to save the exception as string to a variable. Once I could do that, I could split it up and get what was needed from it. This was really easy to do since the ChromeDriver devs had this exception message:

Message: session not created: This version of ChromeDriver only supports Chrome version 86
Current browser version is 88.0.4324.xyz with binary path C:\Program Files (x86)\Google\Chrome\Application\chrome.exe

So I needed to get the 86 to signify the ChromeDriver version and the 88 for the browser version. I've included the code below. If you have any questions, feel free to comment! I'm also aware that I may get crucified for the absolute spaghetti code I've written :). I just want to get something out and then clean up the tomato sauce off my shirt later!!

import logging
logger = logging.Logger('catch_all')

def get_chrome_version():
    global browser_version_number
    try:
        browser_version_driver = webdriver.Chrome("chromedriver_win32/chromedriver.exe")
        browser_version_number = (browser_version_driver.capabilities['browserVersion'])
        browser_version_number = browser_version_number.split(".")[0]

        chromedriverversion = browser_version_driver.capabilities['chrome']['chromedriverVersion'].split('.')[0]
        
        print(browser_version_number)
        print(chromedriverversion)
        
        if browser_version_number != chromedriverversion:
            update_chrome_version()
        
        browser_version_driver.quit()

###############---------------REALLY IMPORTANT PART BELOW---------------###############
    except Exception as e:
        e = str(e) # Saves exception to a variable. Most importantly, converts to string to allow me to manipulate it.
        print(e)

        linesplit = e.split('This version of ChromeDriver only supports Chrome version ')[1]
        checking_driverversion = linesplit.split('\n')[0]
        print(checking_driverversion) # prints '86' which is my chromedriver right now
        
        checking_browserversion = linesplit.split('\n')[1]
        checking_browserversion = checking_browserversion.split('Current browser version is ')[1]
        checking_browserversion = checking_browserversion.split('.')[0]
        print(checking_browserversion) # prints '88' which is my browser version right now
        browser_version_number = checking_browserversion

        if checking_browserversion != checking_driverversion:
            update_chrome_version()
Related