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

WSL环境下Jupyter中Selenium调用Chrome的配置问题

WSL环境下Selenium启动Chrome报错DevToolsActivePort不存在的解决办法

问题场景

在Windows的Linux WSL环境中,使用Jupyter Notebook编写Python爬虫,Selenium 4.6+版本已自动处理chromedriver的下载与匹配,但启动Chrome时仍触发SessionNotCreatedException,核心报错为DevToolsActivePort file doesn't exist,完整错误日志如下:

DEBUG:selenium.webdriver.common.selenium_manager:Selenium Manager binary found at: /home/scaride/.pyenv/versions/taxifare-env/lib/python3.10/site-packages/selenium/webdriver/common/linux/selenium-manager
DEBUG:selenium.webdriver.common.selenium_manager:Executing process: /home/scaride/.pyenv/versions/taxifare-env/lib/python3.10/site-packages/selenium/webdriver/common/linux/selenium-manager --browser chrome --debug --language-binding python --output json
DEBUG:selenium.webdriver.common.selenium_manager:chromedriver not found in PATH
DEBUG:selenium.webdriver.common.selenium_manager:chrome not found in PATH
DEBUG:selenium.webdriver.common.selenium_manager:chrome not found in the system
DEBUG:selenium.webdriver.common.selenium_manager:Required browser: chrome 124.0.6367.201
DEBUG:selenium.webdriver.common.selenium_manager:chrome 124.0.6367.201 already exists
DEBUG:selenium.webdriver.common.selenium_manager:chrome 124.0.6367.201 is available at /home/scaride/.cache/selenium/chrome/linux64/124.0.6367.201/chrome
DEBUG:selenium.webdriver.common.selenium_manager:Required driver: chromedriver 124.0.6367.201
DEBUG:selenium.webdriver.common.selenium_manager:chromedriver 124.0.6367.201 already in the cache
DEBUG:selenium.webdriver.common.selenium_manager:Driver path: /home/scaride/.cache/selenium/chromedriver/linux64/124.0.6367.201/chromedriver
DEBUG:selenium.webdriver.common.selenium_manager:Browser path: /home/scaride/.cache/selenium/chrome/linux64/124.0.6367.201/chrome
DEBUG:selenium.webdriver.common.service:Started executable: `/home/scaride/.cache/selenium/chromedriver/linux64/124.0.6367.201/chromedriver` in a child process with pid: 24249 using 0 to output -3
DEBUG:selenium.webdriver.remote.remote_connection:POST http://localhost:46735/session {'capabilities': {'firstMatch': [{}], 'alwaysMatch': {'browserName': 'chrome', 'pageLoadStrategy': <PageLoadStrategy.normal: 'normal'>, 'browserVersion': None, 'goog:chromeOptions': {'extensions': [], 'binary': '/home/scaride/.cache/selenium/chrome/linux64/124.0.6367.201/chrome', 'args': []}}}}
DEBUG:selenium.webdriver.remote.remote_connection:Remote response: status=500 | data={"value":{"error":"session not created","message":"session not created: Chrome failed to start: exited normally.\n  (session not created: DevToolsActivePort file doesn't exist)\n  (The process started from chrome location /home/scaride/.cache/selenium/chrome/linux64/124.0.6367.201/chrome is no longer running, so ChromeDriver is assuming that Chrome has crashed.)","stacktrace":"#0 0x56122066ee43 \u003Cunknown>\n#1 0x56122035d4e7 \u003Cunknown>\n#2 0x561220391a66 \u003Cunknown>\n#3 0x56122038d9c0 \u003Cunknown>\n#4 0x5612203d67f0 \u003Cunknown>\n#5 0x5612203ca1f3 \u003Cunknown>\n#6 0x56122039b28a \u003Cunknown>\n#7 0x56122039bc5e \u003Cunknown>\n#8 0x56122063316b \u003Cunknown>\n#9 0x5612206370bb \u003Cunknown>\n#10 0x56122061f281 \u003Cunknown>\n#11 0x561220637c22 \u003Cunknown>\n#12 0x56122060413f \u003Cunknown>\n#13 0x56122065df98 \u003Cunknown>\n#14 0x56122065e170 \u003Cunknown>\n#15 0x56122066df94 \u003Cunknown>\n#16 0x7f26a109f609 start_thread\n"}} | headers=HTTPHeaderDict({'Content-Length': '966', 'Content-Type': 'application/json; charset=utf-8', 'cache-control': 'no-cache'})
DEBUG:selenium.webdriver.remote.remote_connection:Finished Request
---------------------------------------------------------------------------
SessionNotCreatedException                Traceback (most recent call last)
Cell In [4], line 3
      1 from selenium import webdriver
----> 3 driver = webdriver.Chrome()
      4 driver.maximize_window()
      5 url = 'https://www.carrefour.com.ar/almacen'

File ~/.pyenv/versions/taxifare-env/lib/python3.10/site-packages/selenium/webdriver/chrome/webdriver.py:45, in WebDriver.__init__(self, options, service, keep_alive)
     42 service = service if service else Service()
     43 options = options if options else Options()
---> 45 super().__init__(
     46     browser_name=DesiredCapabilities.CHROME["browserName"],
     47     vendor_prefix="goog",
     48     options=options,
     49     service=service,
     50     keep_alive=keep_alive,
     51 )

File ~/.pyenv/versions/taxifare-env/lib/python3.10/site-packages/selenium/webdriver/chromium/webdriver.py:66, in ChromiumDriver.__init__(self, browser_name, vendor_prefix, options, service, keep_alive)
     57 executor = ChromiumRemoteConnection(
     58     remote_server_addr=self.service.service_url,
     59     browser_name=browser_name,
   (...)\n     62     ignore_proxy=options._ignore_local_proxy,
     63 )
     65 try:
---> 66     super().__init__(command_executor=executor, options=options)
     67 except Exception:
     68     self.quit()

File ~/.pyenv/versions/taxifare-env/lib/python3.10/site-packages/selenium/webdriver/remote/webdriver.py:208, in WebDriver.__init__(self, command_executor, keep_alive, file_detector, options)
    206 self._authenticator_id = None
    207 self.start_client()
---> 208 self.start_session(capabilities)

File ~/.pyenv/versions/taxifare-env/lib/python3.10/site-packages/selenium/webdriver/remote/webdriver.py:292, in WebDriver.start_session(self, capabilities)
    285 """Creates a new session with the desired capabilities.
    286 
    287 :Args:
    288  - capabilities - a capabilities dict to start the session with.
    289 """
    291 caps = _create_caps(capabilities)
---> 292 response = self.execute(Command.NEW_SESSION, caps)["value"]
    293 self.session_id = response.get("sessionId")
    294 self.caps = response.get("capabilities")

File ~/.pyenv/versions/taxifare-env/lib/python3.10/site-packages/selenium/webdriver/remote/webdriver.py:347, in WebDriver.execute(self, driver_command, params)
    345 response = self.command_executor.execute(driver_command, params)
    346 if response:
---> 347     self.error_handler.check_response(response)
    348     response["value"] = self._unwrap_value(response.get("value", None))
    349     return response

File ~/.pyenv/versions/taxifare-env/lib/python3.10/site-packages/selenium/webdriver/remote/errorhandler.py:229, in ErrorHandler.check_response(self, response)
    227         alert_text = value["alert"].get("text")
    228     raise exception_class(message, screen, stacktrace, alert_text)  # type: ignore[call-arg]  # mypy is not smart enough here
---> 229 raise exception_class(message, screen, stacktrace)

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 /home/scaride/.cache/selenium/chrome/linux64/124.0.6367.201/chrome is no longer running, so ChromeDriver is assuming that Chrome has crashed.)
Stacktrace:
#0 0x56122066ee43 <unknown>
#1 0x56122035d4e7 <unknown>
#2 0x561220391a66 <unknown>
#3 0x56122038d9c0 <unknown>
#4 0x5612203d67f0 <unknown>
#5 0x5612203ca1f3 <unknown>
#6 0x56122039b28a <unknown>
#7 0x56122039bc5e <unknown>
#8 0x56122063316b <unknown>
#9 0x5612206370bb <unknown>
#10 0x56122061f281 <unknown>
#11 0x561220637c22 <unknown>
#12 0x56122060413f <unknown>
#13 0x56122065df98 <unknown>
#14 0x56122065e170 <unknown>
#15 0x56122066df94 <unknown>
#16 0x7f26a109f609 start_thread

解决步骤

1. 添加Chrome无头模式与必要启动参数

WSL默认无图形界面,Chrome需要以无头模式运行,同时添加规避权限和资源限制的参数:

from selenium import webdriver
from selenium.webdriver.chrome.options import Options

chrome_options = Options()
# 启用新版无头模式
chrome_options.add_argument("--headless=new")
# 禁用沙箱模式(规避WSL权限限制)
chrome_options.add_argument("--no-sandbox")
# 禁用/dev/shm使用(解决临时空间不足问题)
chrome_options.add_argument("--disable-dev-shm-usage")
# 指定固定调试端口
chrome_options.add_argument("--remote-debugging-port=9222")

driver = webdriver.Chrome(options=chrome_options)
driver.get("https://www.carrefour.com.ar/almacen")
# 执行爬取操作
driver.quit()

2. 赋予Chrome二进制文件执行权限

Selenium自动下载的Chrome可能缺少执行权限,执行以下命令修复:

chmod +x /home/scaride/.cache/selenium/chrome/linux64/124.0.6367.201/chrome

3. 配置WSL图形界面支持(如需可视化Chrome)

如果需要看到Chrome界面,需先在Windows端安装VcXsrv,然后在WSL终端中设置DISPLAY环境变量:

export DISPLAY=$(cat /etc/resolv.conf | grep nameserver | awk '{print $2}'):0.0

可将该命令添加到~/.bashrc或~/.zshrc中,实现WSL启动时自动配置。

4. 替代方案:改用Firefox

若Chrome问题仍无法解决,可尝试使用Firefox,Selenium同样会自动管理geckodriver:

from selenium import webdriver
from selenium.webdriver.firefox.options import Options

firefox_options = Options()
firefox_options.add_argument("--headless")

driver = webdriver.Firefox(options=firefox_options)
driver.get("https://www.carrefour.com.ar/almacen")
# 执行爬取操作
driver.quit()

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.25 13:57:01