无头铬与硒一起运行 [英] Headless Chrome run with selenium

查看:75
本文介绍了无头铬与硒一起运行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

        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:无法解析远程响应:

未找到

知道为什么吗?

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

推荐答案

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

What are the versions of your Chrome browser, chromedriver and Selenium? I tried with:

  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中,替换:

Note: In current versions of Selenium and ChromeDriver replace:

    chromeOptions.addArguments("--headless");

使用

    chromeOptions.setHeadless(true);

参考:

Ref: https://seleniumhq.github.io/selenium/docs/api/java/org/openqa/selenium/chrome/ChromeOptions.html#setHeadless-boolean- Also you must set Window size otherwise it renders in mobile mode and you may not get certain elements in the page.

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

在带有硒3.14.0的chromedriver 2.42.591071上测试

Tested on chromedriver 2.42.591071 with Selenium 3.14.0

输出:

The Internet

看看无头Chrome入门关于浏览器支持版本.

Take a look to Getting Started with Headless Chrome about the browser support versions.

这篇关于无头铬与硒一起运行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆