【问题标题】:Headless Chrome run with selenium使用硒运行的无头 Chrome
【发布时间】:2018-04-14 11:57:47
【问题描述】:
        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 中的异常: 无法解析远程响应:

未找到

知道为什么吗?

已关注:How to connect to Chromium Headless using Selenium

【问题讨论】:

    标签: java selenium google-chrome-headless


    【解决方案1】:

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

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

    使用 Selenium 3.14.0 在 chromedriver 2.42.591071 上测试

    输出:

    The Internet
    

    查看Getting Started with Headless Chrome 了解浏览器支持版本。

    【讨论】:

    【解决方案2】:
    options.addArguments("headless");
    capabilities.setCapability(ChromeOptions.CAPABILITY, options);
    

    这对我有用。 Chrome驱动版本:2.37

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-01-22
      • 1970-01-01
      • 2018-08-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多