当前位置:网站首页>自动更新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)
边栏推荐
猜你喜欢
DR-Net: dual-rotation network with feature map enhancement for medical image segmentation
Les entreprises ne veulent pas remplacer un système vieux de dix ans
企业不想换掉用了十年的老系统
ACL 2022 | small sample ner of sequence annotation: dual tower Bert model integrating tag semantics
Let's see through the network i/o model from beginning to end
Machine test question 1
【全网首发】Redis系列3:高可用之主从架构的
View
ACL 2022 | 序列标注的小样本NER:融合标签语义的双塔BERT模型
Flutter life cycle
随机推荐
监控界的最强王者,没有之一!
Matlab tips (27) grey prediction
浅谈网络安全之文件上传
Machine test question 1
CSDN 上传图片取消自动加水印的方法
Chapter 19 using work queue manager (2)
European Bioinformatics Institute 2021 highlights report released: nearly 1million proteins have been predicted by alphafold
[step on pit collection] attempting to deserialize object on CUDA device+buff/cache occupy too much +pad_ sequence
MySQL中正则表达式(REGEXP)使用详解
企業不想換掉用了十年的老系統
POJ 1094 sorting it all out
How big is the empty structure?
DevSecOps软件研发安全实践——发布篇
TDengine 社区问题双周精选 | 第二期
(flutter2) as import old project error: inheritfromwidgetofexacttype
What can be done for traffic safety?
Windows Auzre 微软的云计算产品的后台操作界面
让我们,从头到尾,通透网络I/O模型
让 Rust 库更优美的几个建议!你学会了吗?
How to choose indoor LED display? These five considerations must be taken into account