如何修复 SessionNotCreatedException:尝试运行 Firefox 时无法创建新的远程会话?

Posted

技术标签:

【中文标题】如何修复 SessionNotCreatedException:尝试运行 Firefox 时无法创建新的远程会话?【英文标题】:How to fix SessionNotCreatedException: Unable to create new remote session when trying to run Firefox? 【发布时间】:2021-07-19 17:18:22 【问题描述】:

我有一个带有 Chrome 和 Firefox 的 Selenium 服务器 4。 Chrome 工作正常,但当我尝试运行 Firefox 时,它会抛出一个错误:

org.openqa.selenium.SessionNotCreatedException:无法创建新的远程会话。所需功能 = Capabilities acceptInsecureCerts: true, browserName: firefox, marionette: true

驱动的Java代码在这里:

String browser = System.getProperty("browser");
browser = browser == null || browser.equals("") ? CHROME : browser.toLowerCase();

System.setProperty("webdriver.gecko.driver", "/home/tatrytec/custom-scripts/selenium-server/geckodriver");

Configuration.remote = HUB_URL;
Configuration.browser = browser;

FirefoxOptions options = new FirefoxOptions();
options.setCapability("marionette",true);
options.setHeadless(true);
WebDriver driver = new RemoteWebDriver(options);
WebDriverRunner.setWebDriver(driver);

这是 Jenkins 堆栈跟踪:

Caused by: org.openqa.selenium.SessionNotCreatedException: Unable to create new remote session. desired capabilities = Capabilities acceptInsecureCerts: true, browserName: firefox, marionette: true
Build info: version: '3.141.59', revision: 'e82be7d358', time: '2018-11-14T08:17:03'
System info: host: 'vmi503579.contaboserver.net', ip: '127.0.1.1', os.name: 'Linux', os.arch: 'amd64', os.version: '5.4.0-70-generic', java.version: '11.0.10'
Driver info: driver.version: RemoteWebDriver
    at org.openqa.selenium.remote.ProtocolHandshake.createSession(ProtocolHandshake.java:87)
    at org.openqa.selenium.remote.HttpCommandExecutor.execute(HttpCommandExecutor.java:136)
    at org.openqa.selenium.remote.RemoteWebDriver.execute(RemoteWebDriver.java:552)
    at org.openqa.selenium.remote.RemoteWebDriver.startSession(RemoteWebDriver.java:213)
    at org.openqa.selenium.remote.RemoteWebDriver.<init>(RemoteWebDriver.java:131)
    at org.openqa.selenium.remote.RemoteWebDriver.<init>(RemoteWebDriver.java:144)
    at services.WebDriverService.setDriver(WebDriverService.java:51)
    at steps.BaseSteps.<clinit>(BaseSteps.java:43)

这是 Selenium 服务器堆栈跟踪:

12:34:10.493 INFO [Distributor.newSession] - Session request received by the distributor:
 
  "desiredCapabilities": 
    "browserName": "firefox",
    "moz:firefoxOptions": 
      "args": [
        "-headless"
      ],
      "prefs": 
      
    ,
    "marionette": true,
    "acceptInsecureCerts": true
  ,
  "capabilities": 
    "firstMatch": [
      
        "acceptInsecureCerts": true,
        "browserName": "firefox",
        "moz:firefoxOptions": 
          "args": [
            "-headless"
          ],
          "prefs": 
          
        
      
    ]
  

1619433250551   geckodriver     INFO    Listening on 127.0.0.1:21294
1619433251262   mozrunner::runner       INFO    Running command: "/snap/bin/firefox" "--marionette" "-headless" "-foreground" "-no-remote" "-profile" "/tmp/rust_mozprofile8aLKtJ"
2021/04/26 12:34:13.167735 cmd_run.go:1003: WARNING: cannot start document portal: dial unix /run/user/0/bus: connect: permission denied
mkdir: cannot create directory '/run/user/1000': Permission denied
*** You are running in headless mode.
[GFX1-]: glxtest: libpci missing
[GFX1-]: glxtest: libEGL initialize failed
[GFX1-]: glxtest: Unable to open a connection to the X server
[GFX1-]: glxtest: libEGL initialize failed
[GFX1-]: No GPUs detected via PCI
12:35:11.436 WARN [SeleniumSpanExporter$1.lambda$export$0] - "traceId": "276854ae3527ad1a0d5c4039f0580cec","spanId": "2c522f44eb54f30e","spanKind": "INTERNAL","eventTime": 1619433311373994495,"eventName": "exception","attributes": 
"driver.url": "http:\u002f\u002flocalhost:21294",
"exception.message": "Error while creating session with the driver service. Stopping driver service: Could not start a new session. Response code 500. Message: Connection refused (os error 111)\nBuild info: version: '4.0.0-beta-2', revision: 'Unknown'\nSystem info: host: 'vmi123456.contaboserver.net', ip: '127.0.1.1', os.name: 'Linux', os.arch: 'amd64', os.version: '5.4.0-70-generic', java.version: '11.0.10'\nDriver info: driver.version: unknown"

服务器上的权限似乎有问题。它说:

无法启动文档门户:拨打 unix /run/user/0/bus: connect: permission denied

不知道我还应该设置什么来运行。

【问题讨论】:

你是否从 docker 容器运行测试? 另外,您能否分享一下,您是如何找到 selenium 服务器堆栈跟踪的?我遇到了同样的问题,可能会尝试找出根本原因。 要查看 Selenium 服务器堆栈跟踪,您必须停止服务器并手动从控制台运行它。然后你会在控制台看到服务器在做什么。 【参考方案1】:

这在使用 snap 安装的 firefox 中很常见 您可以卸载 Firefox 的 snap 版本并使用 apt by 安装它

snap remove firefox

然后

sudo apt-get install firefox
sudo apt-get install firefox-geckodriver

对此的一些参考 https://github.com/mozilla/geckodriver/issues/1877

https://askubuntu.com/questions/1334192/glxtest-libpci-missing-although-i-made-an-apt-install

【讨论】:

以上是关于如何修复 SessionNotCreatedException:尝试运行 Firefox 时无法创建新的远程会话?的主要内容,如果未能解决你的问题,请参考以下文章

如何修复漏洞

如何修复WMI

PHP网站漏洞怎么修复 如何修补网站程序代码漏洞

如何修复这些漏洞? (npm audit fix 无法修复这些漏洞)

如何修复AppScan漏洞

如何在DOS环境下修复系统