java - 未知错误 : Chrome failed to start: crashed(selenium , headless 模式)

标签 java linux google-chrome selenium-webdriver

我在执行过程中有这段代码和一个问题(代码下的描述)

   try {
        ChromeOptions options = new ChromeOptions();
        options.setBinary("/usr/bin/chromium-browser");
        options.addArguments("--start-maximized");
        options.addArguments("headless");
        DesiredCapabilities capabilities = DesiredCapabilities.chrome();

        capabilities.setBrowserName("chrome");
        capabilities.setPlatform(Platform.LINUX);

        capabilities.setCapability(ChromeOptions.CAPABILITY, options);
        WebDriver driver = null;

        // here throws exception
        driver = new RemoteWebDriver(new URL("http://127.0.0.1:4444/wd/hub"), capabilities);
    }catch (Exception ex){
        int point = 0;
    }

我使用:

  1. Ubuntu 14.04.5 LTS
  2. selenium-server-standalone-3.7.1
  3. 依赖 org.seleniumhq.selenium:selenium-java:3.7.1
  4. ChromeDriver(适用于 Linux 64)
  5. Chromium(而不仅仅是 chrome)
  6. xvfb

当我创建 RemoteWebDriver 时,我得到了这个错误: 异常:未知错误:Chrome 无法启动:崩溃

启动selenium-server的命令

DISPLAY=:1 xvfb-run java  -Dwebdriver.chrome.driver="/home/builds/chromedriver" -jar /home/builds/selenium-server-standalone-3.7.1.jar

该方案的哪些部分可能是导致所述错误的原因?

最佳答案

首先,您输入的所有参数对于 headless 都是错误的。

--headless 不是headless

使用下面的代码

options.addArguments("--headless");

另一件事你必须传递 System.setProperty 如下:

 System.setProperty("webdriver.chrome.driver","E:\\Selenium\\src\\libs\\chromedriver.exe");

我这边运行良好的代码是:

         try {
             System.setProperty("webdriver.chrome.driver","E:\\Selenium\\Workplace\\Selenium\\src\\libs\\chromedriver.exe");
                ChromeOptions options = new ChromeOptions();
            //    options.setBinary("/usr/bin/chromium-browser");
                options.addArguments("--start-maximized");
                options.addArguments("--headless");
                DesiredCapabilities capabilities = DesiredCapabilities.chrome();

                capabilities.setBrowserName("chrome");
                capabilities.setPlatform(Platform.WINDOWS);

                capabilities.setCapability(ChromeOptions.CAPABILITY, options);
                WebDriver driver =  new ChromeDriver(capabilities);
                driver.get("http://google.com");


            }catch (Exception ex){
                int point = 0;
            }

    }

关于java - 未知错误 : Chrome failed to start: crashed(selenium , headless 模式),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47373674/

相关文章:

java - 在 Java EE 项目中,删除我的类后,我无法取回它们?

linux - 如何在 32 位 Linux 上配置用户空间结束地址

javascript - 如何在桌面上调试 WebSocket/服务器发送事件重新连接?

google-chrome - canvasJS 不适用于 mozilla 和 Internet Explorer

java - 如何从类中调用 servlet

java - 用于 PDF 输出的 EPS 到 SVG 转换

java - 必须先发出 STARTTLS 命令

c - 如何将一个库的全局变量使用到另一库

linux - 在 linux 中找到序列/排序被破坏的行

google-chrome - Uploadify 使 Chrome 的 Flashplayer 崩溃并生成 'Ah, snap!' 屏幕