Java org.openqa.selenium.WebDriverException:未知错误:Chrome无法启动:在Ubuntu 18.04的Jenkins中使用ChromeDriver selenium时崩溃

Java org.openqa.selenium.WebDriverException:未知错误:Chrome无法启动:在Ubuntu 18.04的Jenkins中使用ChromeDriver selenium时崩溃,java,google-chrome,jenkins,selenium-webdriver,ubuntu-18.04,Java,Google Chrome,Jenkins,Selenium Webdriver,Ubuntu 18.04,铬在我的詹金斯身上不稳定。当我运行构建5次时,它运行1-2次成功,其余3次出现上述错误 错误快照: Chrome的代码: ChromeOptions options = new ChromeOptions(); System.setProperty("webdriver.chrome.driver","/usr/local/bin/chromedriver"); options.addArguments("--headless");

铬在我的詹金斯身上不稳定。当我运行构建5次时,它运行1-2次成功,其余3次出现上述错误

错误快照:

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");
     
我已经采取了一些步骤:

  • 为google chrome和chrome驱动程序提供777权限

  • Set:在构建之前启动Xvfb,在Jenkins构建设置中设置为True之后关闭它

  • ChromeDriver 81.0.4044.69

  • 谷歌浏览器81.0.4044.129

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

  • 此错误消息

    …表示ChromeDriver无法启动/生成新的浏览上下文,即Chrome浏览器会话


    深潜 查看您提供的错误stacktrace的快照,尽管您提到使用ChromeDriver 81.0.4044.69和Google Chrome 81.0.4044.129,但您使用的不同二进制文件的版本之间似乎不匹配,可能由于JDK不匹配,Chrome浏览器未安装在系统中的默认位置。此外,ChromeDriver 81.0.4044.69(2020-03-17)有点不稳定,被替换为

    但是,服务器即ChromeDriver希望您在每个系统的默认位置安装Chrome,如下图所示:

    1对于Linux系统,ChromeDriver希望
    /usr/bin/google chrome
    是实际chrome二进制文件的符号链接

    您可以在中找到详细的讨论


    解决方案 如果在非标准位置使用Chrome可执行文件,则必须按如下方式覆盖Chrome二进制位置:

    • 基于代码的解决方案:

      System.setProperty("webdriver.chrome.driver", "/path/to/chromedriver");
      ChromeOptions options = new ChromeOptions();
      options.setBinary('/usr/bin/google-chrome');    //chrome binary location
      options.addArguments("--headless");
      options.addArguments("--no-sandbox");
      options.addArguments("--disable-dev-shm-usage");
      WebDriver driver = new ChromeDriver(options);
      driver.get("https://www.google.com/");
      //execute the remaining steps
      driver.quit();
      
    • 其他注意事项-确保:

      • JDK已升级到当前级别
      • 硒被提升到当前水平
      • ChromeDriver已更新到当前级别
      • Chrome更新至当前Chrome版本81.0.4044.138级别。(根据)
      • 通过IDE清理项目工作区,并仅使用所需的依赖项重建项目
      • 以非root用户身份执行
        @Test
      • 始终在
        tearDown(){}
        方法中调用
        driver.quit()
        ,以优雅地关闭和销毁Web驱动程序和Web客户端实例

    工具书类 您可以在以下内容中找到一些相关讨论:


    您是否执行并行测试?如果是,有多少。你试过禁用gpu标志吗?是的@这是一个非常详细的答案。我仍然不确定我做错了什么,但在遵循这一点并重新安装后,一切都正常了。