You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何实现ChromeDriver自动化下载?旧方案失效后的解决方案问询

自动匹配Chrome版本下载ChromeDriver的Python方案

核心思路

从Chrome 115版本开始,ChromeDriver与Chrome浏览器版本完全同步,无需单独查询Driver版本,直接复用本地Chrome的版本号即可匹配对应的ChromeDriver,同时官方改用新的存储库分发ChromeDriver,这也是旧方案失效的核心原因。

实现步骤

1. 获取本地Chrome的版本号

通过调用Chrome命令行参数提取版本信息:

import subprocess
import re

def get_chrome_version():
    try:
        # Windows系统
        result = subprocess.run(
            ["chrome.exe", "--version"],
            capture_output=True,
            text=True,
            check=True
        )
    except FileNotFoundError:
        # macOS系统
        result = subprocess.run(
            ["/Applications/Google Chrome.app/Contents/MacOS/Google Chrome", "--version"],
            capture_output=True,
            text=True,
            check=True
        )
    except:
        # Linux系统
        result = subprocess.run(
            ["google-chrome", "--version"],
            capture_output=True,
            text=True,
            check=True
        )
    # 提取格式类似"Google Chrome 117.0.5938.132"中的版本号
    version_match = re.search(r"\d+\.\d+\.\d+\.\d+", result.stdout)
    if version_match:
        return version_match.group()
    raise ValueError("无法获取Chrome版本号")

2. 适配系统并下载对应ChromeDriver

根据操作系统架构构造下载链接,完成下载、解压及权限配置:

import requests
import os
import zipfile

def download_chromedriver(chrome_version):
    # 识别系统平台
    if os.name == "nt":
        platform = "win32"
        driver_name = "chromedriver.exe"
    elif os.uname().sysname == "Darwin":
        # 区分macOS的Intel和Apple Silicon架构
        platform = "mac-arm64" if os.uname().machine == "arm64" else "mac-x64"
        driver_name = "chromedriver"
    else:
        platform = "linux64"
        driver_name = "chromedriver"
    
    # 构造官方下载链接
    download_url = f"https://storage.googleapis.com/chrome-for-testing-public/{chrome_version}/{platform}/chromedriver-{platform}.zip"
    
    # 流式下载文件
    response = requests.get(download_url, stream=True)
    response.raise_for_status()
    
    # 临时保存压缩包
    zip_path = "chromedriver_temp.zip"
    with open(zip_path, "wb") as f:
        for chunk in response.iter_content(chunk_size=8192):
            f.write(chunk)
    
    # 解压出chromedriver文件
    with zipfile.ZipFile(zip_path, "r") as zip_ref:
        zip_ref.extract(driver_name)
    
    # 为非Windows系统赋予执行权限
    if os.name != "nt":
        os.chmod(driver_name, 0o755)
    
    # 清理临时压缩包
    os.remove(zip_path)
    print(f"ChromeDriver {chrome_version} 下载完成,路径:{os.path.abspath(driver_name)}")

# 主执行流程
if __name__ == "__main__":
    try:
        chrome_ver = get_chrome_version()
        download_chromedriver(chrome_ver)
    except Exception as e:
        print(f"执行出错:{str(e)}")

注意事项

  • 代码自动适配Windows/macOS(含Apple Silicon)/Linux三大主流系统
  • 需提前安装requests库,可通过pip install requests完成安装
  • 若Chrome未安装在默认路径,需手动修改对应系统的Chrome启动命令路径

内容的提问来源于stack exchange,提问作者Marvin

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.10 16:22:17