Selenium 3.141与Python3.11兼容问题:连接超时参数错误
问题原因
Selenium 3.141.0与Python 3.11及新版urllib3存在兼容性冲突:旧版Selenium传递的超时参数为对象类型,而新版urllib3要求超时参数必须是int、float或None类型,因此抛出ValueError: Timeout value connect was <object object at ...>错误。
解决方法
方法1:降级urllib3到兼容版本
执行以下命令安装适配Selenium 3.141.0的urllib3稳定版本,无需修改代码即可解决问题:
pip install urllib3==1.26.15
方法2:修改Selenium源码(仅当无法降级库时使用)
找到本地Selenium包中的remote_connection.py文件,路径参考:C:\Users\yyy\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.11_qbz5n2kfra8p0\LocalCache\local-packages\Python311\site-packages\selenium\webdriver\remote\remote_connection.py
打开文件后,定位到_request方法中的这一行:
resp = self._conn.request(method, url, body=body, headers=headers)
修改为手动指定超时数值的版本:
resp = self._conn.request(method, url, body=body, headers=headers, timeout=10)
将timeout=10中的10替换为你需要的超时秒数即可。
额外优化:修复路径转义问题
虽然不是当前报错的直接原因,但脚本中的路径使用单斜杠可能引发转义异常,建议改为原始字符串格式:
driver = webdriver.Chrome(executable_path=r"C:\Users\yyy\Desktop\paytho\chromedriver.exe")
或双斜杠格式:
driver = webdriver.Chrome(executable_path="C:\\Users\\yyy\\Desktop\\paytho\\chromedriver.exe")
内容的提问来源于stack exchange,提问作者Zerers
相关产品推荐
相关产品推荐

