解决selenium 常见版本不兼容问题

目录

1、解决urllib3库不兼容的问题

2、解决chromedriver与浏览器版本不兼容的问题


【测试环境】

  • selenium:3.141.0
  • Python:3.10

1、解决urllib3库不兼容的问题

背景:在尝试启动selenium时代码出现ValueError错误,代码如下:

python 复制代码
from selenium import webdriver

driver_path = 'E:\PycharmProjects\webUiTest\env\Scripts\chromedriver'
driver = webdriver.Chrome(executable_path=driver_path)
driver.get("https://www.baidu.com")

错误如下:

Traceback (most recent call last): File "E:\PycharmProjects\webUiTest\demo.py", line 10, in <module> driver = webdriver.Chrome(executable_path=driver_path) File "E:\PycharmProjects\webUiTest\env\lib\site-packages\selenium\webdriver\chrome\webdriver.py", line 76, in init RemoteWebDriver.init( File "E:\PycharmProjects\webUiTest\env\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 157, in init self.start_session(capabilities, browser_profile) File "E:\PycharmProjects\webUiTest\env\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 252, in start_session response = self.execute(Command.NEW_SESSION, parameters) File "E:\PycharmProjects\webUiTest\env\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 319, in execute response = self.command_executor.execute(driver_command, params) File "E:\PycharmProjects\webUiTest\env\lib\site-packages\selenium\webdriver\remote\remote_connection.py", line 374, in execute return self._request(command_info[0], url, body=data) File "E:\PycharmProjects\webUiTest\env\lib\site-packages\selenium\webdriver\remote\remote_connection.py", line 397, in _request resp = self._conn.request(method, url, body=body, headers=headers) File "E:\PycharmProjects\webUiTest\env\lib\site-packages\urllib3\_request_methods.py", line 144, in request return self.request_encode_body( File "E:\PycharmProjects\webUiTest\env\lib\site-packages\urllib3\_request_methods.py", line 279, in request_encode_body return self.urlopen(method, url, **extra_kw) File "E:\PycharmProjects\webUiTest\env\lib\site-packages\urllib3\poolmanager.py", line 432, in urlopen conn = self.connection_from_host(u.host, port=u.port, scheme=u.scheme) File "E:\PycharmProjects\webUiTest\env\lib\site-packages\urllib3\poolmanager.py", line 303, in connection_from_host return self.connection_from_context(request_context) File "E:\PycharmProjects\webUiTest\env\lib\site-packages\urllib3\poolmanager.py", line 328, in connection_from_context return self.connection_from_pool_key(pool_key, request_context=request_context) File "E:\PycharmProjects\webUiTest\env\lib\site-packages\urllib3\poolmanager.py", line 351, in connection_from_pool_key pool = self._new_pool(scheme, host, port, request_context=request_context) File "E:\PycharmProjects\webUiTest\env\lib\site-packages\urllib3\poolmanager.py", line 265, in _new_pool return pool_cls(host, port, **request_context) File "E:\PycharmProjects\webUiTest\env\lib\site-packages\urllib3\connectionpool.py", line 195, in init timeout = Timeout.from_float(timeout) File "E:\PycharmProjects\webUiTest\env\lib\site-packages\urllib3\util\timeout.py", line 186, in from_float return Timeout(read=timeout, connect=timeout) File "E:\PycharmProjects\webUiTest\env\lib\site-packages\urllib3\util\timeout.py", line 115, in init self._connect = self._validate_timeout(connect, "connect") File "E:\PycharmProjects\webUiTest\env\lib\site-packages\urllib3\util\timeout.py", line 152, in _validate_timeout raise ValueError( ValueError: Timeout value connect was <object object at 0x0000000A6B6C8720>, but it must be an int, float or None.

根据上面的错误内容来看错误都是有关urllib3库的错误,尤其是最后这里这个错误:

ValueError( ValueError: Timeout value connect was <object object at 0x0000000A6B6C8720>, but it must be an int, float or None.

显然就是版本不兼容导致的,查看urllib3的版本如下:

这个urllib3版本是最新的版本,那我们把版本降低到1.X的版本,因为selenium3.141.0并不是现在最新版本所以会和最新的urllib3库冲突,这里安装urllib3==1.26.8版本的,安装如下:

先卸载最新的版本

pip uninstall urllib3

安装指定版本

pip install urllib3==1.26.8

重新执行就可以正常启动了

2、解决chromedriver与浏览器版本不兼容的问题

1、查看Chrome浏览器的版本

  • 打开Chrome浏览器。
  • 在浏览器地址栏输入chrome://settings/help并回车。
  • 浏览器将显示当前安装的Chrome版本,例如"Google Chrome 版本 127.0.6533.100(正式版本)"。

2、查看ChromeDriver的版本

  • 打开命令行工具(在Windows上是CMD或PowerShell,在Mac或Linux上是Terminal)。
  • 输入chromedriver(如果你已经将ChromeDriver添加到了系统路径中)或./chromedriver或者直接在ChromeDriver的目录下),然后按回车。
  • ChromeDriver将显示其版本信息,类似于"Starting ChromeDriver 1127.0.6533.100 (...) on port 9515"。

3、确认版本是否匹配

  • ChromeDriver的版本号应该与Chrome浏览器的版本号的前三位数字相匹配。例如,如果Chrome浏览器的版本是100.0.4896.127,那么ChromeDriver的版本号应该是100.0.xxxx。
  • 如果ChromeDriver的版本低于Chrome浏览器的版本,你需要下载并安装一个新版本的ChromeDriver。
  • 如果ChromeDriver的版本高于Chrome浏览器的版本太多,也可能会出现兼容性问题。在这种情况下,尝试使用一个更接近浏览器版本的ChromeDriver。

4、下载对应浏览器的ChromeDriver版本

  • 通过上面三个步骤就可以知道对应的ChromeDriver和浏览器是否对应匹配了,如果不匹配则去官网下载对应的ChromeDriver版本。
  • 下载地址:Chrome for Testing availability
  • 下载对应的ChromeDriver版本后解压有两种放置方式:
    • 方式一:放在指定的目录管理,这种方式就需要在webdriver.Chrome(executable_path=driver_path)中指定ChromeDriver的目录。
    • 方式二:和python.exe文件放在同一目录,注意这里python.exe是你项目使用的python.exe,如果你使用的是虚拟环境,那么python.exe就是对应的虚拟环境目录,如下所示:
相关推荐
waterHBO3 小时前
python 爬虫 selenium 笔记
爬虫·python·selenium
chenjingming66611 小时前
windows使用tcpdump.exe工具进行抓包教程
网络·测试工具·tcpdump
小码哥说测试18 小时前
软件测试技术之 GPU 单元测试是什么!
自动化测试·功能测试·测试工具·jmeter·单元测试·集成测试·postman
全能全知者1 天前
不废话简单易懂的Selenium 页面操作与切换
python·selenium·测试工具·网络爬虫
测试19982 天前
使用Selenium进行网页自动化
自动化测试·软件测试·python·selenium·测试工具·自动化·测试用例
做一道光2 天前
1、QAC静态测试常用操作
软件测试·测试工具·静态测试
假女吖☌2 天前
postman接口关联
测试工具·postman
测试杂货铺2 天前
selenium元素定位:元素点击交互异常解决方法
自动化测试·软件测试·python·selenium·测试工具·职场和发展·单元测试
讓丄帝愛伱2 天前
PostMan使用变量
测试工具·postman
可愛小吉3 天前
Python 课程12-Python 自动化应用
开发语言·前端·selenium·openpyxl·reportlab