You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Python执行Selenium脚本遇Timeout值异常,请求技术协助

解决Selenium启动时的ValueError: Timeout value connect异常

问题分析

这个错误的核心是urllib3版本与Selenium版本不兼容:新版本urllib3对超时参数的类型校验更严格,而旧版Selenium传递的超时参数格式不符合要求,触发了类型错误。

解决方案

1. 降级urllib3到兼容版本

执行以下命令安装与旧版Selenium兼容的urllib3稳定版本:

pip install urllib3==1.26.15

注:1.26.x系列是适配旧版Selenium的可靠版本,也可根据实际情况调整为同系列其他版本。

2. 升级Selenium并使用新驱动初始化方式

若不想降级urllib3,建议升级Selenium到4.x版本,使用Service类管理驱动(无需手动指定路径,Selenium会自动识别PATH中的驱动):

Chrome示例代码:

from selenium import webdriver
from selenium.webdriver.chrome.service import Service

# 自动查找PATH中的chromedriver
service = Service()
driver = webdriver.Chrome(service=service)
driver.get("https://www.google.com")
driver.maximize_window()
driver.quit()  # 用quit()替代close(),确保彻底关闭驱动进程

Firefox示例代码:

from selenium import webdriver
from selenium.webdriver.firefox.service import Service

service = Service()
driver = webdriver.Firefox(service=service)
driver.get("https://www.google.com")
driver.maximize_window()
driver.quit()

3. 确认PyCharm虚拟环境配置

检查PyCharm当前使用的Python解释器,确保Selenium、urllib3等依赖安装在项目专属的虚拟环境中,避免全局环境与项目环境的依赖冲突。

内容的提问来源于stack exchange,提问用户yser

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.14 13:35:27