python使用selenium自动化操作浏览器,隔一段时间Chrome自动升级,或者换一台电脑,相同的脚本又不能运行了。出现错误如下错误:
“selenium.common.exceptions.SessionNotCreatedException: Message: session not created: This version of ChromeDriver only supports Chrome version 97
Current browser version is 96.0.4664.45 with binary path C:\Program Files (x86)\Google\Chrome\Application\chrome.exe”
这是因为“ChromeDriver”和浏览器版本不对应所导致的。那么如何解决这个问题呢,
1.手动下载与当前浏览器版本对应的webdriver。网址如下:
http://chromedriver.storage.googleapis.com/index.html
2.每次都下载太麻烦了,那么让代码自动下载与当前浏览器版本匹配的webdriver。
2.1 安装
pip install webdriver-manager
2.2脚本
from selenium import webdriver
from webdriver_manager.chrome import ChromeDriverManager
import os
import sys
sys.path.append(os.path.abspath(''))
from config.config import ConfigManager
# 下载webdriver 到指定的路径
print(ConfigManager.DRIVER_PATH)
driver = webdriver.Chrome(ChromeDriverManager(path=ConfigManager.DRIVER_PATH).install())
运行上面的代码后,就会先检查本地的浏览器版本,然后下载对应的driver版本。
安装第三方包 # pip install webdriver-manager#https://zhuanlan.zhihu.com/p/520759229from selenium import webdriver from webdriver_manager.chrome import ChromeDriverManager import os import sys sys.path.append(os.path.abspath(''))driver_path = r'D:\Program Files\python' driver_path = r'D:\2' # 下载webdriver 到指定的路径 driver = webdriver.Chrome(ChromeDriverManager(path=driver_path).install()) # driver = webdriver.Chrome()# chromedriver下载地址如下 # http://chromedriver.storage.googleapis.com/index.html#版本太高导致报错 # ValueError: There is no such driver by url https://chromedriver.storage.googleapis.com/117.0.5938/chromedriver_win32.zip