【问题标题】:Not able to open Chrome headless from Selenium无法从 Selenium 无头打开 Chrome
【发布时间】:2017-06-27 10:23:26
【问题描述】:

我在这里使用 maven。这是我的 Selenium 代码:​​

DesiredCapabilities capb = DesiredCapabilities.chrome();
    capb.setCapability("chrome.binary","/Applications/Google Chrome.app/Contents/MacOS/Google Chrome");
    ChromeOptions options = new ChromeOptions();
    options.addArguments("--headless","--disable-gpu", "--no-sandbox","--remote-debugging-port=9222");
    capb.setCapability(ChromeOptions.CAPABILITY, options);
    System.setProperty("webdriver.chrome.driver","/Users/nitinkumar/TEST/chromedriver");
    try{
    ChromeDriver driver = new ChromeDriver(capb);
    driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);
    driver.get("https://qa.cmnetwork.co");
    driver.quit();
    }
    catch(Exception e)
    {
        e.printStackTrace();
    }

当我运行“mvn test”时,它会以 GUI 模式启动 chrome。但它应该以无头模式打开。我有 chrome vesrion 59.0OS X yosemite(10.10.5)chromedriver 2.30Selenium 3.4.0 .

【问题讨论】:

  • 值得一提的是Headless mode is available on Mac and Linux in Chrome 59. Windows support is coming in Chrome 60. To check what version of Chrome you have, open chrome://version.
  • 我已经在我的问题中提到过。我现在把它加粗了。
  • 如何设置像driver = new ChromeDriver(options);这样的选项
  • 已尝试但仍以 GUI 模式而不是无头模式打开。

标签: selenium selenium-chromedriver google-chrome-headless


【解决方案1】:

它不会在 GUI 模式下打开。只会打开 chrome 启动器图标。这是一种预期的行为。

您必须删除参数--remote-debugging-port。这将阻止启动的无头 Chrome。所以脚本永远不会前进。你会得到一个chrome not reachable 错误

所以改变参数像

options.addArguments("--headless","--disable-gpu", "--no-sandbox");

另外,不需要--no-sandbox。根据Official doc,只有--headless--disable-gpu 标志就足够了

除非您安装了多个版本的 chrome,否则也不需要 DesiredCapabilities

Headless-chrome 的简单代码

    ChromeOptions options = new ChromeOptions();
    options.addArguments("--headless","--disable-gpu");
    System.setProperty("webdriver.chrome.driver","/Users/nitinkumar/TEST/chromedriver");

    ChromeDriver driver = new ChromeDriver(options);
    driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);
    driver.get("https://qa.cmnetwork.co");
    driver.quit();

【讨论】:

  • 我建议只保留 --no-sandbox。保持 --disable-gpu 参数不会在 Linux 中通过 selenium 显示 chrome UI(根据我的小经验)。
猜你喜欢
  • 2021-06-02
  • 2021-06-10
  • 2020-11-25
  • 1970-01-01
  • 2018-08-25
  • 1970-01-01
  • 2014-10-28
  • 2017-07-07
  • 1970-01-01
相关资源
最近更新 更多