diff --git a/chromedriver_binary/utils.py b/chromedriver_binary/utils.py index 3d9a653..7acd7c6 100644 --- a/chromedriver_binary/utils.py +++ b/chromedriver_binary/utils.py @@ -9,6 +9,8 @@ import subprocess import re import platform +import array +import ctypes try: from urllib.request import urlopen, URLError @@ -111,11 +113,39 @@ def get_chrome_major_version(): if sys.platform == "darwin": browser_executables.insert(0, "/Applications/Google Chrome.app/Contents/MacOS/Google Chrome") + get_major_version = lambda version: re.match(r'.*?((?P\d+)\.(\d+\.){2,3}\d+).*?', version).group('major') + for browser_executable in browser_executables: try: version = subprocess.check_output([browser_executable, '--version']) - return re.match(r'.*?((?P\d+)\.(\d+\.){2,3}\d+).*?', version.decode('utf-8')).group('major') + + return get_major_version(version.decode('utf-8')) + except Exception: + if sys.platform.startswith('win') or sys.platform.startswith('cygwin'): + try: + import winreg + with winreg.OpenKeyEx(winreg.HKEY_CURRENT_USER, r"Software\Google\Chrome\BLBeacon") as key: + version = winreg.QueryValueEx(key, "version")[0] + + return get_major_version(version) + + except Exception: + roots = list(filter(None, [os.getenv('LocalAppData'), os.getenv('ProgramFiles'), os.getenv('ProgramFiles(x86)'), os.getenv('ProgramW6432')])) + + for root in roots: + try: + # https://stackoverflow.com/questions/580924/how-to-access-a-files-properties-on-windows + document = os.path.join(root, 'Google', 'Chrome', 'Application', browser_executable + '.exe') + + name = document.replace(os.path.sep, f'{os.path.sep}{os.path.sep}') + + version = subprocess.check_output(['wmic', 'datafile', 'where', f'name="{name}"', 'get', 'Version', '/value']) + + return get_major_version(version.decode('utf-8').strip()) + + except Exception: + pass pass