Windows11下Selenium启动Chrome无详情WebDriverException求助
Selenium启动Chrome无具体错误消息的解决方法
问题描述
在Windows 11系统中运行以下Python脚本:
from selenium import webdriver from selenium.webdriver.chrome.service import Service options = webdriver.ChromeOptions() options.binary_location = 'C:\\Program Files\\Google\\Chrome\\Application\\chrome.exe' service = Service(executable_path='C:\\ProgramFiles\\Google\\Chrome\\Application\\chromedriver.exe') webC = webdriver.Chrome(options=options, service=service)
运行后抛出无具体描述的WebDriverException,错误栈如下:
Traceback (most recent call last): File "E:/NNs/CrawlerStarter/selenium_test.py", line 8, in <module> webC = webdriver.Chrome(options=options, service=service) File "D:\Anaconda\envs\crawler\lib\site-packages\selenium\webdriver\chrome\webdriver.py", line 69, in __init__ super().__init__(DesiredCapabilities.CHROME['browserName'], "goog", File "D:\Anaconda\envs\crawler\lib\site-packages\selenium\webdriver\chromium\webdriver.py", line 92, in __init__ super().__init__( File "D:\Anaconda\envs\crawler\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 276, in __init__ self.start_session(capabilities, browser_profile) File "D:\Anaconda\envs\crawler\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 369, in start_session response = self.execute(Command.NEW_SESSION, parameters) File "D:\Anaconda\envs\crawler\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 434, in execute self.error_handler.check_response(response) File "D:\Anaconda\envs\crawler\lib\site-packages\selenium\webdriver\remote\errorhandler.py", line 207, in check_response raise exception_class(value) selenium.common.exceptions.WebDriverException: Message: Process finished with exit code 1
已尝试添加系统环境变量Path,切换Firefox浏览器后问题依旧。
环境版本
- Python: 3.8.13
- Selenium: 4.4.0
- Google Chrome: 104.0.5112.81
- ChromeDriver: 104.0.5112.79
解决步骤
1. 修正ChromeDriver路径拼写错误
代码中chromedriver.exe的路径写成了C:\\ProgramFiles\\...,注意ProgramFiles中间缺少空格,正确路径应为C:\\Program Files\\Google\\Chrome\\Application\\chromedriver.exe。路径错误会导致驱动无法被找到,引发无描述异常。
2. 添加ChromeDriver日志输出
在Service对象中添加日志参数,获取启动时的具体错误详情:
service = Service(executable_path='C:\\Program Files\\Google\\Chrome\\Application\\chromedriver.exe', log_path='chromedriver.log')
运行脚本后查看生成的chromedriver.log文件,里面会包含驱动启动失败的具体原因,比如权限问题、端口占用等。
3. 以管理员身份运行脚本
Windows系统中部分场景下,驱动启动需要管理员权限,右键点击Python脚本或终端,选择「以管理员身份运行」重试。
4. 禁用Chrome沙箱模式
向ChromeOptions添加--no-sandbox参数,避免权限限制导致的启动失败:
options.add_argument('--no-sandbox')
5. 验证版本完全兼容性
虽然当前Chrome和ChromeDriver版本接近,但建议下载与Chrome版本完全匹配的ChromeDriver,微小版本差异也可能引发异常。
6. 确认Chrome可执行路径有效性
手动打开options.binary_location指向的chrome.exe路径,验证Chrome本身是否能正常启动,排除浏览器自身故障。
内容的提问来源于stack exchange,提问作者Zevin
相关产品推荐
相关产品推荐

