pu369com

python一招完美搞定Chromedriver的自动更新问题


from webdriver_manager.core.utils import get_browser_version_from_os

from webdriver_manager.chrome import ChromeDriverManager
import requests, re, time, os
from selenium.webdriver.common.by import By

browserVersion = get_browser_version_from_os("google-chrome")  # 获取当前系统chrome浏览器的版本号
mainBrowserVersion = browserVersion.split(".")[0]  # 获取浏览器的主版本号
#resp = requests.get(url="https://chromedriver.storage.googleapis.com/")
resp = requests.get(url="https://chromedriver.storage.googleapis.com/")
content = resp.text
availableVersionList = re.search(
    f"<Contents><Key>({mainBrowserVersion}\.\d+\.\d+\.\d+)/chromedriver_win32\.zip</Key>.*?", content, re.S)
if availableVersionList == None:
    print(f"镜像网站上没有找到主版本号为{mainBrowserVersion}的chromedriver文件,请核实!")
    time.sleep(10)
    os._exit(0)
else:
    availableVersion = availableVersionList.group(1)
driver_path = ChromeDriverManager(version=availableVersion).install()  # 找到镜像网站中


# 测试
import time
from selenium import webdriver

driver = webdriver.Chrome()
driver.get("https://www.baidu.com/")
search_box = driver.find_element(By.NAME, "wd")
search_box.send_keys("Python")
#search_box.submit()
time.sleep(5)

  

期间,遇到了访问https://chromedriver.storage.googleapis.com 超时问题,考虑改为https://npm.taobao.org/mirrors/chromedriver/

还遇到了selenium.common.exceptions.SessionNotCreatedException: Message: session not created: This version of ChromeDriver only supports Chrome version 104问题

原来是由于旧的chromeDriver还在环境变量中,在DOS中 用where chromeDriver 找到并删除之

还有就是Selenium4.X版本变化,请看CHANGES。https://github.com/SeleniumHQ/selenium/blob/a4995e2c096239b42c373f26498a6c9bb4f2b3e7/py/CHANGES

还有 https://www.selenium.dev/blog/2021/announcing-selenium-4/

 

参考:http://news.558idc.com/452965.html

https://blog.csdn.net/yuan2019035055/article/details/125395120

https://betheme.net/yidongkaifa/13900.html?action=onClick

https://www.qiniu.com/qfans/qnso-72773206

https://cloud.tencent.com/developer/article/2085401

posted on 2023-04-12 16:59  pu369com  阅读(571)  评论(0编辑  收藏  举报

导航