使用硒运行的无头 Chrome

ses*_*ses 1 java selenium google-chrome-headless

        System.setProperty("webdriver.chrome.driver", "/usr/bin/google-chrome");

        final ChromeOptions chromeOptions = new ChromeOptions();
        //chromeOptions.addArguments("headless");
        chromeOptions.addArguments("window-size=1200x600");

        final DesiredCapabilities desiredCapabilities = new DesiredCapabilities();
        desiredCapabilities.setCapability(ChromeOptions.CAPABILITY, chromeOptions);


        final URL url = new URL("https://the-internet.herokuapp.com/login");
        final WebDriver driver = new RemoteWebDriver(url, desiredCapabilities);
Run Code Online (Sandbox Code Playgroud)

失败为:

线程“main” org.openqa.selenium.WebDriverException 中的异常:无法解析远程响应:

未找到

知道为什么吗?

后续:如何使用 Selenium 连接到 Chromium Headless

Dav*_*tti 5

您的 Chrome 浏览器、chromedriver 和 Selenium 的版本是什么?我试过:

  1. Chrome 版本 62.0.3202.75(官方版本)(64 位)
  2. 铬驱动程序 2.33
  3. 硒 3.6.0

以下代码:

    System.setProperty("webdriver.chrome.driver", "/pathTo/chromedriver);

    ChromeOptions chromeOptions = new ChromeOptions();
    chromeOptions.addArguments("--headless");

    ChromeDriver driver = new ChromeDriver(chromeOptions);
    driver.get("https://the-internet.herokuapp.com/login");
    System.out.println(driver.getTitle());
Run Code Online (Sandbox Code Playgroud)

注意:在当前版本的 Selenium 和 ChromeDriver 中替换:

    chromeOptions.addArguments("--headless");
Run Code Online (Sandbox Code Playgroud)

    chromeOptions.setHeadless(true);
Run Code Online (Sandbox Code Playgroud)

参考:https : //seleniumhq.github.io/selenium/docs/api/java/org/openqa/selenium/chrome/ChromeOptions.html#setHeadless-boolean- 此外,您必须设置窗口大小,否则它会在移动模式下呈现,您可能无法获取页面中的某些元素。

    chromeOptions.addArguments("--window-size=1200x600");
Run Code Online (Sandbox Code Playgroud)

在 chromedriver 2.42.591071 和 Selenium 3.14.0 上测试

输出:

The Internet
Run Code Online (Sandbox Code Playgroud)

请查看Headless Chrome 入门,了解浏览器支持版本。