Read Chrome Browser Version from Chrome.exe File Using Python

I am a skilled software engineer with expertise in automation engineering, specializing in infrastructure, Test Framework Development and DevOps. With a strong command of Golang, Java, and Python, I have worked across diverse industries including healthcare, human resources, auditing and assurance, manufacturing, and banking. My experience in these industries has allowed me to design and implement efficient automated solutions tailored to their specific needs. As an effective problem-solver and collaborator, I drive automation initiatives and deliver high-quality results across various industry domains.
Sometimes in our automation scripts, we need to know the Chrome version installed on the user's system.
To automate this process via Python code we need to do a few steps.
Navigate to the folder
Read the chrome.exe file
Search for the pattern in the chrome.exe file content.
Let's see How we can achieve the same using re - regular expression module
import re
def get_chrome_version():
chrome_path = "C:/Program Files/Google/Chrome/Application/chrome.exe"
with open(chrome_path, "rb") as f:
content = f.read()
version_pattern = r"([\d]{3})\.([\d]+)\.([\d]+)\.([\d]+)"
match = re.search(version_pattern, str(content))
if match:
return ".".join(match.groups())
else:
print("Failed to find Google Chrome version.")
return ""
if __name__ == '__main__':
version = get_chrome_version()
print(version)
Let's break down the code and explain what it does.
version_pattern = r"([\d]{3})\.([\d]+)\.([\d]+)\.([\d]+)"
Here, a regular expression pattern is defined and stored in the version_pattern variable. The pattern is ([\d]{3})\.([\d]+)\.([\d]+)\.([\d]+). Let's break it down further:
([\d]{3}): This matches exactly three digits. The[\d]is a character class that matches any digit, and{3}specifies that it should match exactly three occurrences of the preceding pattern.\.: This matches a period (dot) character. Since dot (.) has a special meaning in regular expressions, it needs to be escaped with a backslash (\) to match a literal dot.([\d]+): This matches one or more digits.\.([\d]+): This is similar to the previous part, matching a dot followed by one or more digits. This pattern is repeated twice more to match the remaining two parts of the version number.
The regular expression is used to match a version number in the format "xxx.yyy.zzz.www", where each part consists of one or more digits.
match = re.search(version_pattern, str(content))
The re.search() function is called to search for a match of the version_pattern regular expression within the content variable. The str() function is used to convert the content to a string if it's not already a string. The result is stored in the match variable.
if match:
return ".".join(match.groups())
else:
print("Failed to find Google Chrome version.")
return ""
If a match is found (if match:), the code proceeds to the return statement. The match.groups() method returns a tuple containing all the matched groups within the regular expression. join() is called on the tuple to concatenate the groups with periods (dots) in between, resulting in the version number. This version number is then returned from the function.
If no match is found, the code prints a message stating "Failed to find Google Chrome version." and returns an empty string ("").
Below is a sample output after running the program.
114.0.5735.91
Hope you find the snippet and explanation useful.






