Created
March 19, 2025 20:36
-
-
Save yuxi-liu-wired/eca0a544597603ecf418d041b80576ea to your computer and use it in GitHub Desktop.
Python script that downloads the latest stable chromedriver to the current folder
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
import os | |
import requests | |
import zipfile | |
import io | |
import shutil | |
from bs4 import BeautifulSoup | |
def find_chromedriver(root_folder): | |
# Recursively search for chromedriver.exe in the given folder | |
for dirpath, dirnames, filenames in os.walk(root_folder): | |
if "chromedriver.exe" in filenames: | |
return os.path.join(dirpath, "chromedriver.exe") | |
return None | |
def download_chromedriver(): | |
# Step 1: Download the page with versions info | |
base_url = "https://googlechromelabs.github.io/chrome-for-testing/" | |
print("Downloading page:", base_url) | |
response = requests.get(base_url) | |
response.raise_for_status() | |
html = response.text | |
# Step 2: Parse HTML to get the stable version | |
soup = BeautifulSoup(html, 'html.parser') | |
stable_link = soup.find('a', href="#stable", string="Stable") | |
if not stable_link: | |
print("Stable version link not found.") | |
return | |
parent_row = stable_link.find_parent('tr') | |
if not parent_row: | |
print("Parent row not found.") | |
return | |
code_tag = parent_row.find('code') | |
if not code_tag: | |
print("Version code tag not found.") | |
return | |
version = code_tag.get_text().strip() | |
print("Found stable version:", version) | |
# Step 3: Build the download URL and fetch the zip file | |
zip_url = f"https://storage.googleapis.com/chrome-for-testing-public/{version}/win64/chromedriver-win64.zip" | |
print("Downloading chromedriver zip from:", zip_url) | |
zip_response = requests.get(zip_url) | |
zip_response.raise_for_status() | |
# Step 4: Unzip the downloaded file to a folder | |
extract_folder = "chromedriver-win64" | |
with zipfile.ZipFile(io.BytesIO(zip_response.content)) as z: | |
z.extractall(extract_folder) | |
print("Extraction complete.") | |
# Find chromedriver.exe recursively in the extract folder | |
src = find_chromedriver(extract_folder) | |
if not src: | |
print("chromedriver.exe not found in the extracted files.") | |
return | |
# Step 5: Move the chromedriver.exe to the desired folder | |
# Change target_folder to the folder you want the chromedriver in. | |
target_folder = os.getcwd() # Using current working directory | |
dst = os.path.join(target_folder, "chromedriver.exe") | |
shutil.move(src, dst) | |
print(f"chromedriver.exe moved to {dst}") | |
# Clean up: remove the extracted folder | |
shutil.rmtree(extract_folder) | |
print("Temporary files cleaned up.") | |
if __name__ == "__main__": | |
download_chromedriver() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment