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

Github Actions Runner中Selenium启动Chrome遇SessionNotCreatedException

Github Actions Runner中Selenium Chrome测试启动失败:用户数据目录被占用

异常信息

org.openqa.selenium.SessionNotCreatedException: Could not start a new session. Response code 500. Message: session not created: probably user data directory is already in use, please specify a unique value for --user-data-dir argument, or don't use --user-data-dir 
Host info: host: 'road-runner-os023-m-nwx', ip: 'xxx'
Build info: version: '4.23.0', revision: '4df0a231af'
System info: os.name: 'Linux', os.arch: 'amd64', os.version: '5.19.0-41-generic', java.version: '17.0.14'
Driver info: org.openqa.selenium.chrome.ChromeDriver
Command: [null, newSession {capabilities=[Capabilities {browserName: chrome, goog:chromeOptions: {args: [--ignore-certificate-errors, --no-sandbox, --disable-dev-shm-usage, --user-data-dir=/tmp/chrome..., --disable-setuid-sandbox, --no-zygote, --start-maximized, --window-size=1920,1080, --headless=old, --disable-gpu, --window-size=1920,1080, --start-maximized], binary: /usr/bin/google-chrome, excludeSwitches: [enable-automation], extensions: [], prefs: {credentials_enable_service: false, profile.password_manager_enabled: false}, useAutomationExtension: false}}]}]

已尝试但无效的操作

  • 硬编码添加固定路径参数:options.addArguments("--user-data-dir=/tmp/chrome-user-data-dir");
  • 在Github Actions中创建固定目录并设置环境变量:
    run: |
      export USER_DATA_DIR="/tmp/chrome-user-data-dir"
      mkdir -p "$USER_DATA_DIR"
      echo "USER_DATA_DIR=$USER_DATA_DIR" >> $GITHUB_ENV  
    
  • 通过driver = new ChromeDriver(options);启动会话

解决方案

1. 生成唯一临时用户数据目录(代码层面)

在Java代码中自动创建唯一临时目录,避免固定路径冲突:

import java.nio.file.Files;
import java.nio.file.Path;
import java.io.IOException;

// 设置ChromeOptions时生成唯一临时目录
try {
    Path tempDir = Files.createTempDirectory("chrome-user-data-");
    options.addArguments("--user-data-dir=" + tempDir.toAbsolutePath().toString());
} catch (IOException e) {
    e.printStackTrace();
}
driver = new ChromeDriver(options);

每次运行测试都会生成独一无二的目录,从根源避免占用冲突。

2. 移除--user-data-dir参数(推荐)

如果测试不需要保留用户配置、缓存等数据,直接删除该参数即可。ChromeDriver会自动创建临时目录,测试完成后自动清理,无需手动管理:

// 删除options中--user-data-dir相关配置行
driver = new ChromeDriver(options);

3. 在Github Actions中生成唯一目录(必须指定目录时使用)

利用Github Actions内置环境变量生成唯一目录路径,保证每个作业的目录不重复:

run: |
  # 使用GITHUB_RUN_ID生成唯一目录名
  USER_DATA_DIR="/tmp/chrome-user-data-${GITHUB_RUN_ID}"
  mkdir -p "$USER_DATA_DIR"
  echo "USER_DATA_DIR=$USER_DATA_DIR" >> $GITHUB_ENV

代码中读取环境变量:

String userDataDir = System.getenv("USER_DATA_DIR");
if (userDataDir != null) {
    options.addArguments("--user-data-dir=" + userDataDir);
}
driver = new ChromeDriver(options);

4. 清理Runner残留进程与目录

在测试前添加清理步骤,确保之前的Chrome进程和残留目录被清除:

run: |
  # 杀掉所有Chrome相关进程
  pkill -f chrome || true
  pkill -f chromedriver || true
  # 清理所有chrome-user-data开头的临时目录
  rm -rf /tmp/chrome-user-data-* || true

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.14 20:26:15