A Guide to Downloading Chrome Driver Based on Your Chrome Browser using Python.
Table of contents
Introduction:
Automating web browsers has become a common practice for various tasks, from web scraping to automated testing. If you're looking to automate Google Chrome, you'll need ChromeDriver, an essential component that bridges the gap between your automation code and the Chrome browser. In this article, I will guide you through the process of downloading and installing ChromeDriver based on the version of Google Chrome installed on your system. Say goodbye to manual ChromeDriver updates and let's streamline your browser automation!
Code Explanation:
Let's dive into the code that automates the process of downloading ChromeDriver based on the Chrome browser version:
import re
import os
import platform
import requests
import zipfile
def get_chrome_version():
with open("C:/Program Files/Google/Chrome/Application/chrome.exe", "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()).rsplit('.', 1)[0]
else:
print("Failed to find Google Chrome version.")
return ""
def install_chromedriver():
os_name = platform.system()
chrome_version_system = get_chrome_version()
if os_name == "Windows":
chrome_version = requests.get(
"https://chromedriver.storage.googleapis.com/LATEST_RELEASE_{}".format(chrome_version_system)).text
chromedriver_url = f"https://chromedriver.storage.googleapis.com/{chrome_version}/chromedriver_win32.zip"
response = requests.get(chromedriver_url)
with open("chromedriver.zip", "wb") as f:
f.write(response.content)
with zipfile.ZipFile("chromedriver.zip", "r") as zip_ref:
zip_ref.extractall(".")
os.remove("chromedriver.zip")
if __name__ == '__main__':
install_chromedriver()
The code begins by importing the necessary modules. Let's break down their usage:
The
re
module provides functions for working with regular expressions. It is used to extract the version number from thechrome.exe
file.The
os
module provides a way to interact with the operating system. It is used to handle file operations and remove the downloaded zip file.The
platform
module provides access to underlying platform information. It is used to determine the operating system.The
requests
module allows sending HTTP requests. It is used to fetch the latest ChromeDriver version and download the corresponding ZIP fileThe
zipfile
module provides tools for working with ZIP archives. It is used to extract the contents of the downloaded ZIP file.
Next, we have the get_chrome_version()
function, which reads the content of the chrome.exe
file located in the default installation directory of Google Chrome and extracts the version number using regular expressions.
The install_chromedriver()
function is responsible for the actual installation process. It determines the operating system using platform.system()
and retrieves the Chrome version using the get_chrome_version()
function.
If the operating system is Windows, the code uses the Chrome version to retrieve the latest compatible ChromeDriver version from the ChromeDriver website. It then downloads the ChromeDriver ZIP file using the requests.get()
function and saves it as chromedriver.zip
. The zipfile.ZipFile()
class is used to extract the contents of the ZIP file into the current directory.
Finally, the code removes the downloaded ZIP file using os.remove()
to ensure a clean installation.
Conclusion: In this article, we explored how to automate the process of downloading and installing ChromeDriver based on the version of Google Chrome installed on your system. We covered the necessary steps, from determining your Chrome browser version to downloading the appropriate ChromeDriver version and completing the installation. By following these steps, you can effortlessly set up ChromeDriver and unlock the full potential of browser automation. Say hello to streamlined browser automation and take your projects to the next level. Happy automating!