首次使用Python Selenium WebDriver Manager遇OSError: Starting path not found
解决Selenium+Webdriver-Manager启动报错"OSError: Starting path not found"的步骤
1. 清理冲突的Webdriver-Manager版本
从报错栈可见,实际运行的是webdriver_manager-3.8.6-py3.11.egg,但你手动安装的是4.9.0,说明旧版本未被覆盖,存在版本冲突:
- 执行命令卸载旧版本:
pip uninstall webdriver-manager -y
- 手动删除残留文件:打开
C:\Python\Python311\Lib\site-packages,删除所有含webdriver_manager的.egg文件和文件夹
2. 用pip重新安装指定版本
放弃手动setup.py安装方式,改用pip安装更可靠:
pip install webdriver-manager==4.9.0
3. 修复Dotenv路径解析问题
报错根源是Webdriver-Manager加载时,find_dotenv()无法正确定位.env文件。在你的test.py开头添加代码,手动指定.env路径:
import os from dotenv import load_dotenv # 手动绑定当前脚本所在目录的.env文件 env_path = os.path.join(os.path.dirname(__file__), '.env') load_dotenv(env_path) # 原有Selenium代码 from selenium import webdriver from selenium.webdriver.chrome.service import Service from webdriver_manager.chrome import ChromeDriverManager driver = webdriver.Chrome(service=Service(ChromeDriverManager().install())) driver.get("https://www.google.com")
4. 验证运行环境
- 确保执行
python test.py时,命令行的当前工作目录是你的项目目录(即.env和test.py所在目录) - 验证Python版本:
python --version
确认输出为Python 3.11.3
替代方案:跳过Webdriver-Manager直接使用本地ChromeDriver
如果仍有问题,可直接指定本地ChromeDriver路径绕过Webdriver-Manager:
from selenium import webdriver from selenium.webdriver.chrome.service import Service # 替换为你的ChromeDriver实际路径 driver_path = "C:\\your\\path\\to\\chromedriver.exe" driver = webdriver.Chrome(service=Service(driver_path)) driver.get("https://www.google.com")
内容的提问来源于stack exchange,提问作者Argyll
相关产品推荐
相关产品推荐

