org.openqa.selenium.WebDriverException: unknown error: Chrome failed to start: crashed using ChromeDriver Selenium in Jenkins on Ubuntu 18.04

huangapple go评论102阅读模式
英文:

org.openqa.selenium.WebDriverException: unknown error: Chrome failed to start: crashed using ChromeDriver Selenium in Jenkins on Ubuntu 18.04

问题

以下是翻译好的内容:

我的 Jenkins 上 Chrome 不稳定。当我运行构建 5 次时,有 1-2 次成功,而其他 3 次出现了上述错误。

错误截图:
org.openqa.selenium.WebDriverException: unknown error: Chrome failed to start: crashed using ChromeDriver Selenium in Jenkins on Ubuntu 18.04

Chrome 代码:

ChromeOptions options = new ChromeOptions();
System.setProperty("webdriver.chrome.driver", "/usr/local/bin/chromedriver");
options.addArguments("--headless");
options.addArguments("--no-sandbox");
options.addArguments("--disable-dev-shm-usage");
driver = new ChromeDriver(options);
driver.get("https://mywebsite.com");

我已经采取的一些步骤:

  1. 为 Google Chrome 和 Chrome 驱动提供了 777 权限。

  2. 在 Jenkins 构建设置中将 "在构建之前启动 Xvfb,并在构建之后关闭" 设置为 True。
    org.openqa.selenium.WebDriverException: unknown error: Chrome failed to start: crashed using ChromeDriver Selenium in Jenkins on Ubuntu 18.04

  3. ChromeDriver 版本 81.0.4044.69。

  4. Google Chrome 版本 81.0.4044.129。

  5. Ubuntu 18.04.4 LTS (GNU/Linux 4.15.0-99-generic x86_64)

英文:

Chrome is not stable on my Jenkins. When I run build 5 times, it runs 1 - 2-time success, and the other 3 times I have the above error.

Snapshot of the error:
org.openqa.selenium.WebDriverException: unknown error: Chrome failed to start: crashed using ChromeDriver Selenium in Jenkins on Ubuntu 18.04

Code for Chrome :

ChromeOptions options = new ChromeOptions();
System.setProperty("webdriver.chrome.driver","/usr/local/bin/chromedriver");
options.addArguments("--headless");
options.addArguments("--no-sandbox");
options.addArguments("--disable-dev-shm-usage");
driver = new ChromeDriver(options);
driver.get("https://mywebsite.com");

Some steps I have already taken :

  1. Provided 777 permission to google chrome and chrome driver

  2. Set : Start Xvfb before the build, and shut it down after to True in Jenkins build setting
    org.openqa.selenium.WebDriverException: unknown error: Chrome failed to start: crashed using ChromeDriver Selenium in Jenkins on Ubuntu 18.04

  3. ChromeDriver 81.0.4044.69

  4. Google Chrome 81.0.4044.129

  5. Ubuntu 18.04.4 LTS (GNU/Linux 4.15.0-99-generic x86_64)

答案1

得分: 3

这个错误信息...

org.openqa.selenium.WebDriverException: unknown error: Chrome failed to start: crashed using ChromeDriver Selenium in Jenkins on Ubuntu 18.04

...暗示着 ChromeDriver 无法初始化/启动一个新的 浏览上下文,即 Chrome 浏览器 会话。


深入探讨

从您提供的错误堆栈跟踪的快照来看,虽然您提到使用了 ChromeDriver 81.0.4044.69Google Chrome 81.0.4044.129,但仍然似乎在您使用的不同二进制文件版本之间存在不匹配,可能是 Chrome 浏览器未安装在系统的默认位置,或者是由于 JDK 不匹配。此外,ChromeDriver 81.0.4044.69 (2020-03-17) 在某种程度上是不稳定的,已被 ChromeDriver 81.0.4044.138 (2020-05-05) 取代。

然而,服务器即 ChromeDriver 期望您在系统的默认位置安装了 Chrome,如下图所示:

org.openqa.selenium.WebDriverException: unknown error: Chrome failed to start: crashed using ChromeDriver Selenium in Jenkins on Ubuntu 18.04

<sup>1</sup> 对于 Linux 系统,ChromeDriver 期望 /usr/bin/google-chrome 是指向实际 Chrome 二进制文件的符号链接。

> 您可以在 What is default location of ChromeDriver and for installing Chrome on Windows 找到详细的讨论。


解决方案

如果您在非标准位置使用 Chrome 可执行文件,您必须按照以下方式 覆盖 Chrome 二进制文件的位置

  • 基于代码的解决方案:

      System.setProperty("webdriver.chrome.driver", "/path/to/chromedriver");
      ChromeOptions options = new ChromeOptions();
      options.setBinary('/usr/bin/google-chrome');    //chrome 二进制文件的位置
      options.addArguments("--headless");
      options.addArguments("--no-sandbox");
      options.addArguments("--disable-dev-shm-usage");
      WebDriver driver = new ChromeDriver(options);
      driver.get("https://www.google.com/");
      //执行其余步骤
      driver.quit();
    
  • 额外的考虑事项:

    • JDK 升级到当前版本 JDK 8u251
    • Selenium 升级到当前版本 Version 3.141.59
    • ChromeDriver 更新到当前版本 ChromeDriver v81.0.4044.138
    • Chrome 更新到当前版本 Chrome Version 81.0.4044.138(根据 ChromeDriver v80.0 发布说明)。
    • 通过您的 IDE 清理 项目工作区,并仅重新构建带有所需依赖项的项目。
    • 使用 非 root 用户 执行您的 @Test
    • 始终在 tearDown(){} 方法中调用 driver.quit(),以优雅地关闭和销毁 WebDriverWeb Client 实例。

参考资料

您可以在以下链接中找到一些相关的讨论:

英文:

This error message...

org.openqa.selenium.WebDriverException: unknown error: Chrome failed to start: crashed using ChromeDriver Selenium in Jenkins on Ubuntu 18.04

...implies that the ChromeDriver was unable to initiate/spawn a new Browsing Context i.e. Chrome Browser session.


Deep dive

Looking into the snapshot of the error stacktrace you have provided, though you mentioned about using ChromeDriver 81.0.4044.69 and Google Chrome 81.0.4044.129, still it appears there is a mismatch between the versions of the different binaries you are using, possibly Chrome browser is not installed at the default location within your system or due to JDK mismatch. Additionally, ChromeDriver 81.0.4044.69 (2020-03-17) was a bit unstable which was replaced by ChromeDriver 81.0.4044.138 (2020-05-05)

However, the server i.e. ChromeDriver expects you to have Chrome installed in the default location for each system as per the image below:

org.openqa.selenium.WebDriverException: unknown error: Chrome failed to start: crashed using ChromeDriver Selenium in Jenkins on Ubuntu 18.04

<sup>1</sup>For Linux systems, the ChromeDriver expects /usr/bin/google-chrome to be a symlink to the actual Chrome binary.

>You can find a detailed discussion in What is default location of ChromeDriver and for installing Chrome on Windows


Solution

In case you are using the Chrome executable in a non-standard location you have to override the Chrome binary location as follows:

  • Code based solution:

      System.setProperty(&quot;webdriver.chrome.driver&quot;, &quot;/path/to/chromedriver&quot;);
      ChromeOptions options = new ChromeOptions();
      options.setBinary(&#39;/usr/bin/google-chrome&#39;);	//chrome binary location
      options.addArguments(&quot;--headless&quot;);
      options.addArguments(&quot;--no-sandbox&quot;);
      options.addArguments(&quot;--disable-dev-shm-usage&quot;);
      WebDriver driver = new ChromeDriver(options);
      driver.get(&quot;https://www.google.com/&quot;);
      //execute the remaining steps
      driver.quit();
    
  • Additional considerations- Ensure the following:

  • JDK is upgraded to current levels JDK 8u251.

  • Selenium is upgraded to current levels Version 3.141.59.

  • ChromeDriver is updated to current ChromeDriver v81.0.4044.138 level.

  • Chrome is updated to current Chrome Version 81.0.4044.138 level. (as per ChromeDriver v80.0 release notes)

  • Clean your Project Workspace through your IDE and Rebuild your project with required dependencies only.

  • Execute your @Test as non-root user.

  • Always invoke driver.quit() within tearDown(){} method to close & destroy the WebDriver and Web Client instances gracefully.


References

You can find a couple of relevant discussions in:

huangapple
  • 本文由 发表于 2020年5月5日 15:17:52
  • 转载请务必保留本文链接:https://go.coder-hub.com/61607750.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定