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

Selenium测试失败:Chrome无法在非无头模式启动求助

环境

  • Azure ADO经典流水线
  • 微软托管代理 构建信息:version: '4.14.1', revision: '03f8ede370'
  • 系统信息:os.name:'Linux', os.arch: 'amd64', os.version: '6.2.0-1018-azure'

问题

测试启动时出现以下异常:

Could not start a new session. Response code 500. Message: session not created: Chrome failed to start: exited normally.
 
(session not created: DevToolsActivePort file doesn't exist)

经典流水线YAML代码

# Variable 'baseURL' was defined in the Variables tab
# Variable 'browser' was defined in the Variables tab
variables:
- name: BuildParameters.mavenPOMFile
  value: pom.xml
trigger:
  branches:
    include:
    - refs/heads/main
name: $(date:yyyyMMdd)$(rev:.r)
resources:
  repositories:
  - repository: self
    type: git
    ref: main
jobs:
- job: Job_1
  displayName: Agent job 1
  pool:
    vmImage: ubuntu-latest
  steps:
  - checkout: self
    clean: true
    fetchTags: false
  - task: JavaToolInstaller@0
    displayName: Use Java 17
    inputs:
      versionSpec: 17
      jdkArchitectureOption: x64
      jdkSourceOption: PreInstalled
  - task: MavenAuthenticate@0
    displayName: Authenticate for Azure Artifacts
    inputs:
      artifactsFeeds: azure-artifacts
  - task: Maven@4
    displayName: Build bdd-test
    inputs:
      mavenPOMFile: $(BuildParameters.mavenPOMFile)
      goals: verify
      options: --no-transfer-progress -DskipITs=false -DbaseURL=$(baseURL) -Dbrowser=$(browser)
  - task: NodeTool@0
    displayName: Install Node for Cucumber
    inputs:
      versionSpec: 12.x
  - task: MaciejMaciejewski.azure-pipelines-cucumber.PublishCucumberReport.PublishCucumberReport@1
    displayName: Publish Cucumber Report
    inputs:
      jsonDir: bdd-test/target/cucumber
      outputPath: bdd-test/target/cucumber
      metadata: >-
        {
          "Base URL": "$(baseURL)",
          "Browser": "$(browser)"
        }
      name: BDD
      title: Test
...

ChromeDriver日志

[1703011948.085][INFO]: Use the --remote-debugging-pipe Chrome switch instead of the default --remote-debugging-port to enable this communication mode.
[1703011948.085][INFO]: Launching chrome: /opt/google/chrome/chrome --allow-pre-commit-input --disable-background-networking --disable-client-side-phishing-detection --disable-default-apps --disable-dev-shm-usage --disable-hang-monitor --disable-popup-blocking --disable-prompt-on-repost --disable-sync --enable-automation --enable-logging --ignore-certificate-errors --log-level=0 --no-first-run --no-sandbox --no-service-autorun --password-store=basic --remote-debugging-port=0 --test-type=webdriver --use-mock-keychain --user-data-dir=/tmp/.org.chromium.Chromium.A6NuzC data:,
[1929:1929:1219/185228.380273:ERROR:ozone_platform_x11.cc(239)] Missing X server or $DISPLAY
[1929:1929:1219/185228.380300:ERROR:env.cc(257)] The platform failed to initialize.  Exiting.
[1219/185228.408089:ERROR:nacl_helper_linux.cc(354)] NaCl helper process running without a sandbox!
Most likely you need to configure your SUID sandbox correctly
[1703011948.448][INFO]: [30db2e7b6389e60968ecef7e960cfdf9] RESPONSE InitSession ERROR session not created: Chrome failed to start: exited normally.
  (session not created: DevToolsActivePort file doesn't exist)
  (The process started from chrome location /opt/google/chrome/chrome is no longer running, so ChromeDriver is assuming that Chrome has crashed.)
[1703011948.448][DEBUG]: Log type 'driver' lost 0 entries on destruction
[1703011948.448][DEBUG]: Log type 'browser' lost 0 entries on destruction

解决方案

从日志里的Missing X server or $DISPLAY错误可知,Linux代理无图形环境,Chrome无法正常启动,需按以下方式配置:

  1. 启用Chrome无头模式
    修改Maven@4任务的options字段,追加无头模式相关系统属性:

    options: --no-transfer-progress -DskipITs=false -DbaseURL=$(baseURL) -Dbrowser=$(browser) -Dwebdriver.chrome.options="--headless=new --disable-gpu --window-size=1920,1080"
    
  2. 校验Chrome与ChromeDriver版本匹配
    微软托管代理的Chrome版本会自动更新,需确保测试依赖的ChromeDriver版本与之一致。可在流水线中添加步骤查看版本:

    - script: |
        google-chrome --version
        chromedriver --version
      displayName: Check Chrome and ChromeDriver versions
    

    若版本不匹配,可使用Maven的webdrivermanager插件自动管理ChromeDriver版本,或手动安装对应版本。

  3. 补充优化启动参数
    若问题仍存在,可在Chrome启动参数中添加--disable-extensions、--disable-infobars等参数,进一步优化启动配置。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.04 00:33:12