当前位置:网站首页>自动更新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)
边栏推荐
- European Bioinformatics Institute 2021 highlights report released: nearly 1million proteins have been predicted by alphafold
- None of the strongest kings in the monitoring industry!
- CSDN 上传图片取消自动加水印的方法
- TDengine 社区问题双周精选 | 第二期
- Thinkphp5 multi table associative query method join queries two database tables, and the query results are spliced and returned
- What are the specific steps and schedule of IELTS speaking?
- 存币生息理财dapp系统开发案例演示
- 浅谈网络安全之文件上传
- Efficient ETL Testing
- 前置机是什么意思?主要作用是什么?与堡垒机有什么区别?
猜你喜欢

Aardio - construct a multi button component with customplus library +plus

(flutter2) as import old project error: inheritfromwidgetofexacttype

Designed for decision tree, the National University of Singapore and Tsinghua University jointly proposed a fast and safe federal learning system

室内LED显示屏应该怎么选择?这5点注意事项必须考虑在内

儿童睡衣(澳大利亚)AS/NZS 1249:2014办理流程

Les entreprises ne veulent pas remplacer un système vieux de dix ans

Mysql 身份认证绕过漏洞(CVE-2012-2122)
MySQL中正则表达式(REGEXP)使用详解

Dayu200 experience officer runs the intelligent drying system page based on arkui ETS on dayu200

Motion capture for snake motion analysis and snake robot development
随机推荐
QT信号和槽
The ceiling of MySQL tutorial. Collect it and take your time
COSCon'22 社区召集令来啦!Open the World,邀请所有社区一起拥抱开源,打开新世界~
hdu 5077 NAND(暴力打表)
UE4 blueprint learning chapter (IV) -- process control forloop and whileloop
浅谈网络安全之文件上传
BasicVSR_ Plusplus master test videos and pictures
金融人士必读书籍系列之六:权益投资(基于cfa考试内容大纲和框架)
Redis 持久化机制
Rust knowledge mind map XMIND
The difference between enumeration and define macro
Interview question: AOF rewriting mechanism, redis interview must ask!!!
Is "applet container technology" a gimmick or a new outlet?
前置机是什么意思?主要作用是什么?与堡垒机有什么区别?
视图(view)
Enterprises do not want to replace the old system that has been used for ten years
CRMEB商城系统如何助力营销?
欧洲生物信息研究所2021亮点报告发布:采用AlphaFold已预测出近1百万个蛋白质
服务器的系统怎么选者
存币生息理财dapp系统开发案例演示