这个解决方案使用默认的Python库,根据您当前本地安装的Chrome版本从互联网下载最匹配的版本。我的Linux笔记本电脑上加载了Google Chrome 97.0.4692.99版本,但是从
link驱动程序中找到的最接近的版本是97.0.4692.71。如果需要,您还可以更改
chrome_driver_url
参数中
latest的值以强制下载最新的Chrome版本。
import os
import ssl, shutil, re, platform
import zipfile
from urllib.request import urlopen
from pathlib import Path
import difflib
def chrome_driver_url(latest=False):
def current_chrome_version():
CHROME_RELEASE_URL = "https://sites.google.com/chromium.org/driver/downloads?authuser=0"
try:
response = urlopen(CHROME_RELEASE_URL,context=ssl.SSLContext(ssl.PROTOCOL_TLS)).read()
except ssl.SSLError:
response = urlopen(CHROME_RELEASE_URL,).read()
downloading_version = re.findall(b"ChromeDriver \d{2,3}\.0\.\d{4}\.\d+", response)
downloading_version = [x.decode().split(" ")[1] for x in downloading_version]
downloading_version.sort(key=lambda x: [int(i) if i.isdigit() else i for i in x.split('.')])
downloading_version.reverse()
osname = platform.system()
if osname == 'Darwin':
installpath = "/Applications/Google\ Chrome.app/Contents/MacOS/Google\ Chrome"
verstr = os.popen(f"{installpath} --version").read().strip('Google Chrome ').strip()
ver_to_download = difflib.get_close_matches(verstr, downloading_version)
ver_to_download = ver_to_download[0]
return ver_to_download
elif osname == 'Windows':
verstr = os.popen('reg query "HKEY_CURRENT_USER\Software\Google\Chrome\BLBeacon" /v version').read().strip().split(" ")
verstr = verstr[-1]
ver_to_download = difflib.get_close_matches(verstr, downloading_version)
ver_to_download = ver_to_download[0]
return ver_to_download
elif osname == 'Linux':
installpath = "/usr/bin/google-chrome"
verstr = os.popen(f"{installpath} --version").read().strip('Google Chrome ').strip()
ver_to_download = difflib.get_close_matches(verstr, downloading_version)
ver_to_download = ver_to_download[0]
return ver_to_download
else:
raise NotImplemented(f"Unknown OS '{osname}'")
if latest:
CHROME_RELEASE_URL = "https://sites.google.com/chromium.org/driver/downloads?authuser=0"
try:
response = urlopen(CHROME_RELEASE_URL, context=ssl.SSLContext(ssl.PROTOCOL_TLS)).read()
except ssl.SSLError:
response = urlopen(CHROME_RELEASE_URL).read()
downloading_version = re.findall(b"ChromeDriver \d{2,3}\.0\.\d{4}\.\d+", response)[0].decode().split()[1]
else:
downloading_version = current_chrome_version()
system = platform.system()
if system == "Windows":
url = f"https://chromedriver.storage.googleapis.com/{downloading_version}/chromedriver_win32.zip"
elif system == "Darwin":
if platform.processor() == 'arm':
url = f"https://chromedriver.storage.googleapis.com/{downloading_version}/chromedriver_mac64_m1.zip"
else:
url = f"https://chromedriver.storage.googleapis.com/{downloading_version}/chromedriver_mac64.zip"
elif system == "Linux":
url = f"https://chromedriver.storage.googleapis.com/{downloading_version}/chromedriver_linux64.zip"
return url
def download_chrome_driver(drivers_dir):
driver_name = "chromedriver.exe" if platform.system() == "Windows" else "chromedriver"
if (drivers_dir / driver_name).exists():
return
url = chrome_driver_url()
try:
response = urlopen(url, context=ssl.SSLContext(ssl.PROTOCOL_TLS))
except ssl.SSLError:
response = urlopen(url)
zip_file_path = drivers_dir / Path(url).name
with open(zip_file_path, 'wb') as zip_file:
while True:
chunk = response.read(1024)
if not chunk:
break
zip_file.write(chunk)
extracted_dir = drivers_dir / zip_file_path.stem
with zipfile.ZipFile(zip_file_path, "r") as zip_file:
zip_file.extractall(extracted_dir)
os.remove(zip_file_path)
driver_path = drivers_dir / driver_name
try:
(extracted_dir / driver_name).rename(driver_path)
except FileExistsError:
(extracted_dir / driver_name).replace(driver_path)
shutil.rmtree(extracted_dir)
os.chmod(driver_path, 0o755)
if __name__ == "__main__":
chrome_driver_location = Path("chrome_driver")
chrome_driver_location.mkdir(exist_ok=True)
download_chrome_driver(chrome_driver_location)
def get_version_via_com(filename): parser = Dispatch("Scripting.FileSystemObject") version = parser.GetFileVersion(filename) return version if __name__ == "__main__": path = r"C:\Program Files\Google\Chrome\Application\chrome.exe" version_number = get_version_via_com(path)```
- supputuri