2

我在这里使用 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

4

1 回答 1

3

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

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

所以改变像这样的论点

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

此外,不需要--no-sandbox. 仅根据官方文档--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();
于 2017-06-27T12:53:35.817 回答