Last active
March 22, 2024 12:04
-
-
Save primaryobjects/d5346bf7a173dbded1a70375ff7461b4 to your computer and use it in GitHub Desktop.
Detect the version of Chrome installed on Windows, Linux, Mac. Cross-platform using Python, native OS detection, does not require Selenium.
This file contains 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. | |
# | |
import os | |
import re | |
from sys import platform | |
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 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
@primaryobjects Thank you for the script, it was quite helpful, actually, it was so helpful that I have created a package based on this script.
The repository of the package is here: hasansezertasan/chrome-version: Detect the version of Chrome installed on Windows, Linux, Mac. Cross-platform using Python, native OS detection.
I have put the link to this gist in the "Disclaimer" section.
🙏