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

Python Selenium中ChromeDriver无法启动及Windows11浏览器推荐

问题描述

此前正常运行的Python Selenium代码在更新ChromeDriver到113版本后出现异常,代码如下:

#Loading Webdriver
options = webdriver.ChromeOptions()
# options.add_argument('--headless')

#Set download folder to newly created folder
prefs = {"download.default_directory" : newfilepath}
options.add_experimental_option("prefs", prefs)
driver = webdriver.Chrome(executable_path=r'C:\Users\ujcho\Desktop\chromedriver.exe', options=options)

wait = WebDriverWait(driver,10)

#Calibrate url to find corresponding file in Karpel
url = "[insert url link here]"
driver.get(url)
login(driver)

奇怪的是,添加--headless参数时程序能正常运行,但注释掉该参数后会报错:

Traceback (most recent call last):
  File "c:\Users\ujcho\Desktop\StanfordLabs2\test.py", line 80, in <module>
    driver = webdriver.Chrome(executable_path=r'C:\Users\ujcho\Desktop\StanfordLabs2\chromedriver.exe', options=options)
             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\ujcho\AppData\Local\Programs\Python\Python311\Lib\site-packages\selenium\webdriver\chrome\webdriver.py", line 84, in __init__
    super().__init__(
  File "C:\Users\ujcho\AppData\Local\Programs\Python\Python311\Lib\site-packages\selenium\webdriver\chromium\webdriver.py", line 104, in __init__
    super().__init__(
  File "C:\Users\ujcho\AppData\Local\Programs\Python\Python311\Lib\site-packages\selenium\webdriver\remote\webdriver.py", line 286, in __init__
    self.start_session(capabilities, browser_profile)
  File "C:\Users\ujcho\AppData\Local\Programs\Python\Python311\Lib\site-packages\selenium\webdriver\remote\webdriver.py", line 378, in start_session
    response = self.execute(Command.NEW_SESSION, parameters)
               ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\ujcho\AppData\Local\Programs\Python\Python311\Lib\site-packages\selenium\webdriver\remote\webdriver.py", line 440, in execute
    self.error_handler.check_response(response)
  File "C:\Users\ujcho\AppData\Local\Programs\Python\Python311\Lib\site-packages\selenium\webdriver\remote\errorhandler.py", line 245, in check_response
    raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.WebDriverException: Message: unknown error: Chrome failed to start: exited normally.
  (unknown error: DevToolsActivePort file doesn't exist)
  (The process started from chrome location C:\Program Files\Google\Chrome\Application\chrome.exe is no longer running, so ChromeDriver is assuming that Chrome has crashed.)

此前代码会打开新窗口执行自动化操作,现在仅在现有窗口打开显示data;的空标签。已重装Google Chrome但问题未解决,询问Windows11系统下适合Python Selenium的浏览器有哪些。

问题修复与浏览器推荐

Chrome问题的解决步骤

针对DevToolsActivePort file doesn't exist错误,可尝试以下方案:

  1. 补充启动参数:给ChromeOptions添加参数,解决沙箱和共享内存限制:
    options.add_argument('--no-sandbox')
    options.add_argument('--disable-dev-shm-usage')
    options.add_argument('--remote-debugging-port=9222')
    
  2. 使用Service类调用驱动:Selenium 4.x推荐用Service替代直接指定executable_path,避免路径和版本适配问题:
    from selenium.webdriver.chrome.service import Service
    
    service = Service(r'C:\Users\ujcho\Desktop\chromedriver.exe')
    driver = webdriver.Chrome(service=service, options=options)
    
  3. 严格匹配版本:确保ChromeDriver版本与本地Chrome浏览器版本完全一致,不要跨版本混用。
  4. 清理残留进程:打开任务管理器,结束所有Chrome相关进程后重新运行程序。

Windows11下适配Python Selenium的浏览器

  • Google Chrome:生态最成熟,文档和社区资源丰富,只要版本匹配ChromeDriver就能稳定运行,是自动化测试首选。
  • Microsoft Edge:基于Chromium内核,与Chrome API完全兼容,Windows11自带,EdgeDriver使用逻辑和ChromeDriver一致,无需额外学习成本,兼容性好。
  • Mozilla Firefox:独立Gecko内核,GeckoDriver驱动稳定,适合需要适配Firefox的场景。
  • Opera:Chromium内核,OperaDriver支持,适合特定测试需求。
  • Brave:Chromium内核,主打隐私保护,直接用ChromeDriver即可驱动,适合隐私相关的自动化任务。

内容的提问来源于stack exchange,提问作者jocho

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.21 14:07:09