繁体   English   中英

无头铬与硒一起运行

[英]Headless Chrome run with selenium

        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);

失败为:

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

未找到

知道为什么吗?

后续: 如何使用Selenium连接到无头的Chromium

您的Chrome浏览器,chromedriver和Selenium有哪些版本? 我尝试了:

  1. Chrome版本62.0.3202.75(正式版本)(64位)
  2. chromedriver 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());

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

    chromeOptions.addArguments("--headless");

    chromeOptions.setHeadless(true);

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

    chromeOptions.addArguments("--window-size=1200x600");

在带有硒3.14.0的chromedriver 2.42.591071上测试

输出:

The Internet

查看有关浏览器支持版本的Headless Chrome入门

options.addArguments("headless");
capabilities.setCapability(ChromeOptions.CAPABILITY, options);

这对我有用。 Chromedriver版本:2.37

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM