当前位置:网站首页>自动更新Selenium驱动chromedriver

自动更新Selenium驱动chromedriver

2022-07-06 15:40:00 小小明-代码实体

由于谷歌游览器经常自动更新导致selenium驱动失效需要重新下载,如何让代码自动更新selenium驱动的方法呢?

selenium连接谷歌游览器抛出异常时,会展示当前谷歌游览器的版本信息,我们可以到selenium驱动下载的网站获取全部版本信息,然后找个一个匹配的版本进行下载并解压即可。

为了更快的下载速度,这里我选择国内的镜像站:http://npm.taobao.org/mirrors/chromedriver/

它会重定向到:https://registry.npmmirror.com/binary.html?path=chromedriver/

经过开发者工具检查,可以找到获取最新的全部版本信息的接口:https://registry.npmmirror.com/-/binary/chromedriver/

我按照前三个号作为键进行匹配,下载小版本号最大的作为驱动。

最终完整代码为:

from selenium import webdriver
from selenium.common.exceptions import SessionNotCreatedException
import re
import os
import requests
import zipfile
import itertools


def getChromeDriver(options=None):
    """代码作者:小小明-代码实体 xxmdmst.blog.csdn.net"""
    try:
        driver = webdriver.Chrome(options=options)
        return driver
    except SessionNotCreatedException as e:
        driver_version = re.search(
            "Chrome version ([\d.]+)", str(e)).group(1)
        chrome_version = re.search(
            "Current browser version is ([\d.]+) with", str(e)).group(1)
        print(f"驱动版本:{
      driver_version},谷歌游览器版本:{
      chrome_version},不兼容\n开始更新驱动...")
        res = requests.get(
            "https://registry.npmmirror.com/-/binary/chromedriver/")
        versions = [obj["name"][:-1] for obj in res.json() if re.match("\d+",
                                                                       obj["name"]) and obj["name"].count(".") == 3]
        versions = {
    key: max(versions_split, key=lambda x: int(x[x.rfind(".")+1:]))
                    for key, versions_split in itertools.groupby(versions, key=lambda x: x[:x.rfind(".")])}
        dest_version = versions[chrome_version[:chrome_version.rfind(".")]]
        print("驱动将更新到", dest_version)
        file = f"chromedriver_{
      dest_version}_win32.zip"
        if not os.path.exists(file):
            url = f"https://registry.npmmirror.com/-/binary/chromedriver/{
      dest_version}/chromedriver_win32.zip"
            print("驱动下载地址:", url)
            res = requests.get(url)
            with open(file, 'wb') as f:
                f.write(res.content)
        else:
            print(file, "文件已经下载到当前目录,下面直接使用缓存解压覆盖...")
        with zipfile.ZipFile(file) as zf:
            zf.extract("chromedriver.exe", ".")
        driver = webdriver.Chrome(options=options)
        return driver


options = webdriver.ChromeOptions()
options.add_experimental_option(
    'excludeSwitches', ['enable-logging', 'enable-automation'])
driver = getChromeDriver(options)
driver.get("https://www.baidu.com/")

驱动不兼容时的打印示例:

驱动版本:100,谷歌游览器版本:102.0.5005.115,不兼容
开始更新驱动...
驱动将更新到 102.0.5005.61
驱动下载地址: https://registry.npmmirror.com/-/binary/chromedriver/102.0.5005.61/chromedriver_win32.zip

上面自己开发的下载方法下载速度相对是比较快的,如果嫌弃代码太长,可以使用webdriver_manager这个库,通过pip可以直接安装:

pip install webdriver_manager

然后执行以下代码即可从 https://chromedriver.storage.googleapis.com自动下载匹配的驱动,并返回下载位置:

from webdriver_manager.chrome import ChromeDriverManager

ChromeDriverManager().install()

打印示例:

[WDM] - ====== WebDriver manager ======
[WDM] - Current google-chrome version is 102.0.5005
[WDM] - Get LATEST chromedriver version for 102.0.5005 google-chrome
[WDM] - There is no [win32] chromedriver for browser 102.0.5005 in cache
[WDM] - About to download new driver from https://chromedriver.storage.googleapis.com/102.0.5005.61/chromedriver_win32.zip
[WDM] - Driver has been saved in cache [C:\Users\ASUS\.wdm\drivers\chromedriver\win32\102.0.5005.61]
'C:\\Users\\ASUS\\.wdm\\drivers\\chromedriver\\win32\\102.0.5005.61\\chromedriver.exe'

这样我们可以直接通过返回的字符串设置驱动的位置:

from webdriver_manager.chrome import ChromeDriverManager

driver = webdriver.Chrome(ChromeDriverManager().install(), options=None)
原网站

版权声明
本文为[小小明-代码实体]所创,转载请带上原文链接,感谢
https://xxmdmst.blog.csdn.net/article/details/125540557