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

GitLab集成Selenoid时Safari浏览器无法正常工作求助

GitLab中Selenoid运行Safari自动化测试时无法建立连接,提示会话未找到

Chrome、Edge和Firefox浏览器在GitLab CI中通过Selenoid运行自动化测试均正常,但Safari浏览器无法建立连接,出现「会话未找到」及类似错误。

相关代码片段

1. gitlab-ci.yml

test:
    stage: test
    image: docker:latest
    before_script:
      - apt-get update && apt-get install -y maven
      - apk add --update --no-cache curl && rm -rf /var/cache/apk/*
      - docker pull maven:3.8-openjdk-11
      - ls $CI_PROJECT_DIR
      - >
        docker run --rm
        -v /builds:/builds
        -v /var/run/docker.sock:/var/run/docker.sock
        -v ${HOME}:/root
        -e OVERRIDE_HOME=${HOME}
        aerokube/cm:latest-release
        selenoid start
        --browsers-json /builds/myprojectpath/browsers.json
        --args "-limit 1"
    script:
        - docker ps -a
        - docker images -a
        - curl docker:4444/status
        - curl http://docker:4444/status
        - curl -XPOST http://docker:4444/wd/hub/session -d '{"desiredCapabilities":{"browserName":"safari"}}'
        - docker ps -a
        - >
          docker run --rm --network host -v "$(pwd)":/app -w /app maven:3.8-openjdk-11 mvn ${MAVEN_OPTS} test -Dcucumber.filter.tags=${FEATURE_TAG} -Dbrowser=safari

2. browsers.json

{
  "safari": {
    "default": "15.0",
    "versions": {
      "15.0": {
        "image": "browsers/safari:15.0",
        "port": "4444",
        "path": "/"
      }
    }
  }
}

3. Java驱动初始化代码

RemoteWebDriver driver;
SafariOptions options = new SafariOptions();
try {
    driver = new RemoteWebDriver(new URL("http://localhost:4444/wd/hub"), options);
    driver.manage().timeouts().implicitlyWait(Duration.ofSeconds(120));
} catch (MalformedURLException e) {
    throw new RuntimeException(e);
}

错误日志

1. Selenoid日志

$ docker run --rm -v /builds:/builds -v /var/run/docker.sock:/var/run/docker.sock -v ${HOME}:/root -e OVERRIDE_HOME=${HOME} aerokube/cm:latest-release selenoid start --browsers-json /builds/myprojectpath/browsers.json --args "-limit 1"
Unable to find image 'aerokube/cm:latest-release' locally
latest-release: Pulling from aerokube/cm
Status: Downloaded newer image for aerokube/cm:latest-release
> Using Docker
- Your Docker API version is 1.41
> Downloading Selenoid...
- Fetching tags for image aerokube/selenoid
registry.ping url=https://index.docker.io/v2/
registry.tags url=https://index.docker.io/v2/aerokube/selenoid/tags/list repository=aerokube/selenoid
- Pulling image aerokube/selenoid:1.10.11
> Configuring Selenoid...
> Requested to sync configuration from "/builds/myprojectpath/browsers.json"...
- Pulling image browsers/safari:15.0
> Pulling video recorder image...
- Pulling image selenoid/video-recorder:latest-release
> Configuration saved to /root/.aerokube/selenoid/browsers.json
> Starting Selenoid...
> Successfully started Selenoid
$ docker ps -a
CONTAINER ID   IMAGE                       COMMAND                  CREATED         STATUS                  PORTS                    NAMES
abcd4a4bc123   aerokube/selenoid:1.10.11   "/usr/bin/selenoid -…"   2 seconds ago   Up Less than a second   0.0.0.0:4444->4444/tcp   selenoid
$ docker images -a
REPOSITORY                TAG              IMAGE ID       CREATED         SIZE
aerokube/selenoid         1.10.11          ab1a2bcd3456   4 days ago      17.5MB
aerokube/cm               latest-release   123a01b23456   6 weeks ago     17.9MB
maven                     3.8-openjdk-11   1a2bc3defg45   8 months ago    664MB
browsers/safari           15.0             a1b234567890   17 months ago   932MB
selenoid/video-recorder   latest-release   654z321456y7   2 years ago     37.6MB
$ curl docker:4444/status
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed

  0     0    0     0    0     0      0      0 --:--:-- --:--:-- --:--:--     0
{"total":1,"used":0,"queued":0,"pending":0,"browsers":{"safari":{"15.0":{}}}}
100    78  100    78    0     0  27358      0 --:--:-- --:--:-- --:--:-- 39000
$ curl http://docker:4444/status
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed

  0     0    0     0    0     0      0      0 --:--:-- --:--:-- --:--:--     0{"total":1,"used":0,"queued":0,"pending":0,"browsers":{"safari":{"15.0":{}}}}

100    78  100    78    0     0  28581      0 --:--:-- --:--:-- --:--:-- 39000
$ curl -XPOST http://docker:4444/wd/hub/session -d '{"desiredCapabilities":{"browserName":"safari"}}'
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed

  0     0    0     0    0     0      0      0 --:--:-- --:--:-- --:--:--     0
100    48    0     0  100    48      0     39  0:00:01  0:00:01 --:--:--    39
100   115  100    67  100    48     39     28  0:00:01  0:00:01 --:--:--    68
{"value":{"error":"invalid argument","message":"","stacktrace":""}}

2. Java驱动初始化错误日志

org.openqa.selenium.NoSuchSessionException: session deleted because of page crash or hang.
Build info: version: '4.8.0', revision: '267030adea'
System info: os.name: 'Linux', os.arch: 'amd64', os.version: '4.14.311-233.529.amzn2.x86_64', java.version: '11.0.16'
Driver info: org.openqa.selenium.remote.RemoteWebDriver

已尝试无效的解决方案

  • 在browsers.json中添加tmpfs键值对
  • 为SafariOptions设置acceptInsecureCerts为true
  • 删除browsers.json中的path键值对

解决方案尝试

1. 修正浏览器能力配置格式

Selenium 4+推荐使用capabilities而非旧的desiredCapabilities,旧格式可能导致Selenoid无法正确解析。调整curl请求为:

curl -XPOST http://docker:4444/wd/hub/session -d '{"capabilities":{"alwaysMatch":{"browserName":"safari"}}}'

同时修改Java代码,明确指定浏览器版本避免匹配问题:

SafariOptions options = new SafariOptions();
options.setBrowserVersion("15.0");

2. 调整browsers.json的端口与路径配置

Safari Docker镜像的WebDriver服务默认端口为5555,路径为/wd/hub,修改browsers.json:

{
  "safari": {
    "default": "15.0",
    "versions": {
      "15.0": {
        "image": "browsers/safari:15.0",
        "port": "5555",
        "path": "/wd/hub",
        "tmpfs": {"/tmp": "rw"}
      }
    }
  }
}

3. 修复GitLab CI中的网络问题

Maven容器使用--network host时,localhost指向容器自身而非宿主机,修改Java代码中的连接地址与curl请求一致:

driver = new RemoteWebDriver(new URL("http://docker:4444/wd/hub"), options);

4. 增加Selenoid日志级别排查问题

启动Selenoid时添加-log-level debug参数,获取更详细的会话创建日志:

docker run --rm
-v /builds:/builds
-v /var/run/docker.sock:/var/run/docker.sock
-v ${HOME}:/root
-e OVERRIDE_HOME=${HOME}
aerokube/cm:latest-release
selenoid start
--browsers-json /builds/myprojectpath/browsers.json
--args "-limit 1 -log-level debug"

5. 替换为兼容的Safari镜像

browsers/safari:15.0镜像较旧,可能与当前Selenoid版本不兼容,尝试使用更新的官方维护镜像。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.24 19:27:17