当前位置:网站首页>自动更新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)
边栏推荐
- Uniapp setting background image effect demo (sorting)
- docker中mysql开启日志的实现步骤
- Mysql 身份认证绕过漏洞(CVE-2012-2122)
- [untitled]
- memcached
- View
- Interview question: AOF rewriting mechanism, redis interview must ask!!!
- Modules that can be used by both the electron main process and the rendering process
- [unity] upgraded version · Excel data analysis, automatically create corresponding C classes, automatically create scriptableobject generation classes, and automatically serialize asset files
- Puppeter connects to the existing Chrome browser
猜你喜欢

Rust knowledge mind map XMIND

#DAYU200体验官# 首页aito视频&Canvas绘制仪表盘(ets)

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

Efficient ETL Testing

European Bioinformatics Institute 2021 highlights report released: nearly 1million proteins have been predicted by alphafold

Custom swap function

同构+跨端,懂得小程序+kbone+finclip就够了!

MySQL authentication bypass vulnerability (cve-2012-2122)

View

企业不想换掉用了十年的老系统
随机推荐
BasicVSR_PlusPlus-master测试视频、图片
Matlab tips (27) grey prediction
视图(view)
第十九章 使用工作队列管理器(二)
TDengine 社区问题双周精选 | 第二期
ICLR 2022 | pre training language model based on anti self attention mechanism
前置机是什么意思?主要作用是什么?与堡垒机有什么区别?
What can be done for traffic safety?
Puppeteer连接已有Chrome浏览器
Motion capture for snake motion analysis and snake robot development
How to use flexible arrays?
mysql拆分字符串作为查询条件的示例代码
Extern keyword
专为决策树打造,新加坡国立大学&清华大学联合提出快速安全的联邦学习新系统
欧洲生物信息研究所2021亮点报告发布:采用AlphaFold已预测出近1百万个蛋白质
Volatile keyword
[launched in the whole network] redis series 3: high availability of master-slave architecture
使用云服务器搭建代理
Financial professionals must read book series 6: equity investment (based on the outline and framework of the CFA exam)
Introduction to network basics