无头镀铬与硒一起运行

时间:2017-11-02 03:37:32

标签: 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);

失败为:

  

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

未找到

知道为什么吗?

关注:How to connect to Chromium Headless using Selenium

2 个答案:

答案 0 :(得分:3)

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

  1. Chrome版本62.0.3202.75(官方版本)(64位)
  2. chromedriver 2.33
  3. Selenium 3.6.0
  4. 以下代码:

        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- 此外,您必须设置窗口大小,否则它将在移动模式下呈现,您可能无法获得页面中的某些元素。

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

    使用Selenium 3.14.0

    在chromedriver 2.42.591071上测试

    输出:

    The Internet
    

    查看有关浏览器支持版本的Getting Started with Headless Chrome

答案 1 :(得分:1)

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

这对我有用。 Chromedriver版本:2.37

相关问题