非无头模式下Python Selenium4 ChromeDriver崩溃,报SessionNotCreatedException
Selenium4+ChromeDriver非无头模式启动失败排查与解决
问题描述
- 基于Python Selenium4与ChromeDriver的脚本,非无头模式无法启动,但无头模式可正常运行,同代码库其他使用相同导入的脚本无崩溃问题。
- 该脚本与其他爬虫的唯一差异:运行前未删除Chromium配置文件。
报错信息
Traceback (most recent call last): File "/path/to/selenium_scraper.py", line 92, in <module> driver = webdriver.Chrome(options=options) File "/usr/local/lib/python3.9/dist-packages/selenium/webdriver/chrome/webdriver.py", line 45, in __init__ super().__init__( File "/usr/local/lib/python3.9/dist-packages/selenium/webdriver/chromium/webdriver.py", line 66, in __init__ super().__init__(command_executor=executor, options=options) File "/usr/local/lib/python3.9/dist-packages/selenium/webdriver/remote/webdriver.py", line 208, in __init__ self.start_session(capabilities) File "/usr/local/lib/python3.9/dist-packages/selenium/webdriver/remote/webdriver.py", line 292, in start_session response = self.execute(Command.NEW_SESSION, caps)["value"] File "/usr/local/lib/python3.9/dist-packages/selenium/webdriver/remote/webdriver.py", line 347, in execute self.error_handler.check_response(response) File "/usr/local/lib/python3.9/dist-packages/selenium/webdriver/remote/errorhandler.py", line 229, in check_response raise exception_class(message, screen, stacktrace) selenium.common.exceptions.SessionNotCreatedException: Message: session not created: Chrome failed to start: exited normally. (session not created: DevToolsActivePort file doesn't exist) (The process started from chrome location /usr/bin/chromium is no longer running, so ChromeDriver is assuming that Chrome has crashed.) Stacktrace: #0 0x55b34ba06f83 <unknown> #1 0x55b34b6bfcf7 <unknown> #2 0x55b34b6f760e <unknown> #3 0x55b34b6f426e <unknown> #4 0x55b34b74480c <unknown> #5 0x55b34b738e53 <unknown> #5 0x55b34b700dd4 <unknown> #7 0x55b34b7021de <unknown> #8 0x55b34b9cb531 <unknown> #9 0x55b34b9cf455 <unknown> #10 0x55b34b9b7f55 <unknown> #11 0x55b34b9d00ef <unknown> #12 0x55b34b99b99f <unknown> #13 0x55b34b9f4008 <unknown> #14 0x55b34b9f41d7 <unknown> #15 0x55b34ba06124 <unknown> #16 0x7f961961fea7 start_thread
解决方案
1. 强制指定DevTools端口与独立用户目录
通过配置ChromeOptions,避免旧配置文件干扰和端口分配失败:
from selenium import webdriver from selenium.webdriver.chrome.options import Options options = Options() # 指定固定的DevTools调试端口 options.add_argument("--remote-debugging-port=9222") # 设置独立的用户数据目录,隔离原有配置 options.add_argument("--user-data-dir=/tmp/selenium_chrome_profile") # 解决Linux环境下的沙箱限制 options.add_argument("--no-sandbox") # 禁用/dev/shm使用,避免内存不足问题 options.add_argument("--disable-dev-shm-usage") driver = webdriver.Chrome(options=options)
2. 清理旧配置中的DevToolsActivePort文件
如果需要保留原有用户配置,可在启动前手动删除残留的端口文件:
rm -f /path/to/your/chromium_profile/DevToolsActivePort
3. 验证Chrome与ChromeDriver版本匹配
确保Chromium浏览器版本与ChromeDriver版本完全一致,版本不匹配会导致启动失败。可通过以下命令查看版本:
# 查看Chromium版本 chromium --version # 查看ChromeDriver版本 chromedriver --version
4. 禁用GPU加速(针对Linux环境)
部分Linux环境下非无头模式可能因GPU问题崩溃,添加参数禁用GPU:
options.add_argument("--disable-gpu")
内容的提问来源于stack exchange,提问作者Gilles Quénot
相关产品推荐
相关产品推荐

