当前位置:网站首页>自动更新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)
边栏推荐
- Pytest unit test series [v1.0.0] [pytest execute unittest test case]
- How to choose the server system
- (shuttle) navigation return interception: willpopscope
- What can be done for traffic safety?
- Detailed explanation of ThreadLocal
- 欧洲生物信息研究所2021亮点报告发布:采用AlphaFold已预测出近1百万个蛋白质
- (flutter2) as import old project error: inheritfromwidgetofexacttype
- 使用云服务器搭建代理
- 第十九章 使用工作队列管理器(二)
- Sizeof keyword
猜你喜欢

Children's pajamas (Australia) as/nzs 1249:2014 handling process

Case recommendation: An Qing works with partners to ensure that the "smart court" is more efficient

DR-Net: dual-rotation network with feature map enhancement for medical image segmentation

Matlab tips (27) grey prediction

Aardio - Method of batch processing attributes and callback functions when encapsulating Libraries

Flutter life cycle

#DAYU200体验官# 在DAYU200运行基于ArkUI-eTS的智能晾晒系统页面

ICLR 2022 | 基于对抗自注意力机制的预训练语言模型
docker中mysql开启日志的实现步骤

Cloud native technology container knowledge points
随机推荐
Traversal of a tree in first order, middle order, and then order
Custom swap function
Is "applet container technology" a gimmick or a new outlet?
Graphite document: four countermeasures to solve the problem of enterprise document information security
How to achieve text animation effect
Uniapp setting background image effect demo (sorting)
TypeScript获取函数参数类型
Is there any requirement for the value after the case keyword?
让 Rust 库更优美的几个建议!你学会了吗?
On file uploading of network security
MySQL中正则表达式(REGEXP)使用详解
cuda 探索
Return keyword
OpenNMS分离数据库
Const keyword
Demonstration of the development case of DAPP system for money deposit and interest bearing financial management
企业不想换掉用了十年的老系统
Face recognition class attendance system based on paddlepaddle platform (easydl)
DockerMySQL无法被宿主机访问的问题解决
ACL 2022 | 序列标注的小样本NER:融合标签语义的双塔BERT模型