-
-
Save primaryobjects/d5346bf7a173dbded1a70375ff7461b4 to your computer and use it in GitHub Desktop.
# | |
# 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 |
Anyone looking for this in Java ☕ I've written one here, tested on windows & ubuntu 👍
https://gist.github.com/EricBallard/41909b42b6913955e4667b27dc30cb53
This doesn't work on Arch Linux. I installed chrome from Arch AUR
It seems the chrome version that got installed is "google-chrome-stable" instead of "google-chrome"
It's still an easy fix.
Thanks for the script!
great! Thanks a lot 👍
amazing man worked like a charm
@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.
🙏
get_chrome_version()
'91.0.4472.164'