无外设镶边 + 忽略证书错误

我需要获得无外设镶边才能忽略证书错误。在无外设模式下运行时,将忽略该选项,并且驱动程序在导航到 https 资源时返回空的 html 正文标记。

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

此线程确认在无外设模式下忽略。--ignore-certificate-errors

他们提到了devtool协议

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


答案 1

有一篇关于 medium.com 的优秀文章 由 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();

软件/框架版本

  • 谷歌浏览器版本 64.0.3282.186
  • 谷歌浏览器驱动程序版本64.0.3282.186
  • 硒 版本 3.11.0

答案 2

这在ChromeDriver 80上适用于我。

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

推荐