无头镀铬+忽略证书错误

时间:2017-08-04 15:51:40

标签: java google-chrome selenium google-chrome-devtools selenium-chromedriver

我需要无头镀铬来忽略证书错误。在无头模式下运行时,该选项被忽略,当导航到https资源时,驱动程序返回空的html body标签。

<html xmlns="http://www.w3.org/1999/xhtml"><head></head><body></body></html>

这就是我配置Chrome驱动程序的方式。

 ChromeOptions chromeOptions = new ChromeOptions();
 chromeOptions.addArguments("--headless", "--disable-gpu", "--window-size=1920,1200","--ignore-certificate-errors");

 DesiredCapabilities cap=DesiredCapabilities.chrome();

 cap.setCapability(ChromeOptions.CAPABILITY, chromeOptions);
 cap.setCapability(CapabilityType.ACCEPT_SSL_CERTS, true);
 cap.setCapability(CapabilityType.ACCEPT_INSECURE_CERTS, true);
 chromeHeadlessDriver = new ChromeDriver(cap);

This thread确认在无头模式下忽略--ignore-certificate-errors

他们提到了devtool protocol

我可以从java调用吗?还有其他选择吗?

3 个答案:

答案 0 :(得分:5)

medium.com by sahajamit

上有一篇很好的文章

我测试了下面的代码,它与自签名证书https://badssl.com/

完全吻合
    ChromeOptions options = new ChromeOptions();
    options.setExperimentalOption("useAutomationExtension", false);
    options.addArguments("--headless", "--window-size=1920,1200","--ignore-certificate-errors");

    DesiredCapabilities crcapabilities = DesiredCapabilities.chrome();
    crcapabilities.setCapability(ChromeOptions.CAPABILITY, options);
    crcapabilities.setCapability(CapabilityType.ACCEPT_SSL_CERTS, true);
    crcapabilities.setCapability(CapabilityType.ACCEPT_INSECURE_CERTS, true);

    System.setProperty(ChromeDriverService.CHROME_DRIVER_LOG_PROPERTY, "C:\\temp\\chrome\\chromedriver.log");
    System.setProperty(ChromeDriverService.CHROME_DRIVER_EXE_PROPERTY, "C:\\temp\\chrome\\chromedriver.exe");

    ChromeDriverService service = null;
    try {
        service = new ChromeDriverService.Builder()
                .usingAnyFreePort()
                .withVerbose(true)
                .build();
        service.start();
    } catch (IOException e) {
        e.printStackTrace();
    }

    RemoteWebDriver driver = new RemoteWebDriver(service.getUrl(),crcapabilities);

    driver.get("https://self-signed.badssl.com/");
    System.out.println(driver.getPageSource());
    driver.quit();

软件/框架版本

  • Google Chrome版本64.0.3282.186
  • Google Chrome驱动程序版本64.0.3282.186
  • Selenium 3.11.0版

答案 1 :(得分:1)

这对我在ChromeDriver 80上有效。

ChromeOptions option = new ChromeOptions();
option.AddArgument("--headless");
option.AcceptInsecureCertificates = true;
driver = new ChromeDriver(option);

答案 2 :(得分:0)

@ amila-kumara正在运行,但是使用DesiredCapabilities.chrome()会警告使用 ChromeOptions 。请查看更新后的答案。

设置镶边选项值

System.setProperty("webdriver.chrome.driver", Config.NDAC_WEBDRIVER_PATH);
ChromeOptions options = new ChromeOptions();
options.addArguments("--window-size=1920,1200");
options.setAcceptInsecureCerts(true);
options.setHeadless(true);

启动服务

ChromeDriverService service = null;
try {
    service = new ChromeDriverService.Builder()
            .usingAnyFreePort()
            .withVerbose(true)
            .build();
    service.start();
    remoteWebdriverUrl = service.getUrl();
    System.out.println("Starting the url " + remoteWebdriverUrl);
} catch (IOException e) {
    e.printStackTrace();
}

注意:在关闭驱动程序(使用RemoteWebDriver)时遇到了问题,即使您使用 driver.quit(),chromedriver.exe进程也无法关闭。要解决此问题,请使用 ChromeDriver而不是RemoteWebDriver

RemoteWebDriver driver = new ChromeDriver(service, options);

要正确关闭驱动程序,请使用

driver.close();
driver.quit();
相关问题