java 无头 chrome + 忽略证书错误

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/45510973/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-03 08:43:41  来源:igfitidea点击:

Headless chrome + ignore-certificate-errors

javagoogle-chromeseleniumgoogle-chrome-devtoolsselenium-chromedriver

提问by miklesw

I need to get headless chrome to ignore certificate errors. The option is ignored when running in headless mode, and the driver returns empty html body tags when navigating to an https resource.

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

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

This is how I am configuring my chrome driver.

这就是我配置 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 threadconfirms that --ignore-certificate-errorsis ignored in headless mode.

该线程确认--ignore-certificate-errors在无头模式下被忽略。

They mention about devtool protocol.

他们提到了devtool 协议

Is it something I can invoke from java? Are there any other alternatives?

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

回答by Amila kumara

There is an excellent article on medium.com by sahajamit

sahajamit 在 medium.com上有一篇很棒的文章

and i have tested the below code, it works perfectly fine with self-signed certificate https://badssl.com/

我已经测试了下面的代码,它与自签名证书一起工作得很好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();

software/framework versions

软件/框架版本

  • Google Chrome Version 64.0.3282.186
  • Google Chrome Driver Version 64.0.3282.186
  • Selenium version 3.11.0
  • 谷歌浏览器版本 64.0.3282.186
  • Google Chrome 驱动程序版本 64.0.3282.186
  • 硒版本 3.11.0

回答by Vaisakh Rajagopal

@amila-kumara is working but, usage of DesiredCapabilities.chrome()gives warning to use ChromeOptions. Please see the updated answer.

@amila-kumara 正在工作,但是使用DesiredCapabilities.chrome()警告使用ChromeOptions。请参阅更新的答案。

Set the chrome option values

设置镀铬选项值

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);

Start the service

启动服务

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();
}

Note:I was facing the issue while closing the driver(with RemoteWebDriver), chromedriver.exe process won't close even when you use driver.quit(). To fix the issue use ChromeDriver instead of RemoteWebDriver

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

RemoteWebDriver driver = new ChromeDriver(service, options);

To properly close the driver, use

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

driver.close();
driver.quit();

回答by Saurabh Khare

This works for me on ChromeDriver 80.

这在 ChromeDriver 80 上对我有用。

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