-
-
Save randomaccess3/1a3a6cfe6236ab00069a3f1447d82e6f to your computer and use it in GitHub Desktop.
Identify the correct chromedriver-version to download
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # | |
| # Programmatically detect the version of the Chrome web browser installed on the PC. | |
| # Compatible with Windows, Mac, Linux. | |
| # Written in Python. | |
| # Uses native OS detection. Does not require Selenium nor the Chrome web driver. | |
| # Forked version of https://gist.github.com/primaryobjects/d5346bf7a173dbded1a70375ff7461b4 | |
| # Modified to obtain the right version of the selenium driver | |
| # Doesn't support getting the M1 Mac version | |
| # TODO: Add downloading and unpacking the chromedriver to a given directory | |
| import os | |
| import re | |
| from sys import platform | |
| import requests | |
| def extract_version_registry(output): | |
| try: | |
| google_version = '' | |
| for letter in output[output.rindex('DisplayVersion REG_SZ') + 24:]: | |
| if letter != '\n': | |
| google_version += letter | |
| else: | |
| break | |
| return(google_version.strip()) | |
| except TypeError: | |
| return | |
| def extract_version_folder(): | |
| # Check if the Chrome folder exists in the x32 or x64 Program Files folders. | |
| for i in range(2): | |
| path = 'C:\\Program Files' + (' (x86)' if i else '') +'\\Google\\Chrome\\Application' | |
| if os.path.isdir(path): | |
| paths = [f.path for f in os.scandir(path) if f.is_dir()] | |
| for path in paths: | |
| filename = os.path.basename(path) | |
| pattern = '\d+\.\d+\.\d+\.\d+' | |
| match = re.search(pattern, filename) | |
| if match and match.group(): | |
| # Found a Chrome version. | |
| return match.group(0) | |
| return None | |
| def get_chrome_version(): | |
| version = None | |
| install_path = None | |
| try: | |
| if platform == "linux" or platform == "linux2": | |
| # linux | |
| install_path = "/usr/bin/google-chrome" | |
| elif platform == "darwin": | |
| # OS X | |
| install_path = "/Applications/Google\ Chrome.app/Contents/MacOS/Google\ Chrome" | |
| elif platform == "win32": | |
| # Windows... | |
| try: | |
| # Try registry key. | |
| stream = os.popen('reg query "HKLM\\SOFTWARE\\Wow6432Node\\Microsoft\\Windows\\CurrentVersion\\Uninstall\\Google Chrome"') | |
| output = stream.read() | |
| version = extract_version_registry(output) | |
| except Exception as ex: | |
| # Try folder path. | |
| version = extract_version_folder() | |
| except Exception as ex: | |
| print(ex) | |
| version = os.popen(f"{install_path} --version").read().strip('Google Chrome ').strip() if install_path else version | |
| return version | |
| def find_correct_chromedriver_url(): | |
| zipname = "" | |
| version = get_chrome_version() | |
| try: | |
| if platform == "linux" or platform == "linux2": | |
| zipname = "chromedriver_linux64.zip" | |
| elif platform == "darwin": | |
| zipname = "chromedriver_mac64.zip" | |
| elif platform == "win32": | |
| zipname = "chromedriver_win32.zip" | |
| except Exception as ex: | |
| print(ex) | |
| v = requests.get("https://chromedriver.storage.googleapis.com/LATEST_RELEASE_" + version.split(".")[0]).text | |
| url = "https://chromedriver.storage.googleapis.com/" + v + "/" + zipname | |
| return url | |
| print (find_correct_chromedriver_url()) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment