忽略无头Chrome中的证书错误

时间:2018-07-26 08:03:31

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

我使用WebDriver 3.12。我有两个来自应用程序的实例,一个实例无效,另一个实例有效的SSL认证。 Testsuite正在使用有效证书之一正常工作。 --ignore-certificate-errorsChromeDriver 2.40中的无头模式下将被忽略。我也尝试过--acceptInsecureCerts标志,但效果相同。 我将驱动程序变量初始化如下:

WebDriver driver = new ChromeDriver(new ChromeOptions().setExperimentalOption("prefs", prefs).addArguments("--start-maximized").addArguments("--acceptInsecureCerts").setHeadless(true));

2 个答案:

答案 0 :(得分:2)

答案 1 :(得分:1)

您可以尝试以下对我有用的代码段:

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:\\Path\\TO\\chromedriver.log");
System.setProperty(ChromeDriverService.CHROME_DRIVER_EXE_PROPERTY, "C:\\Path\\TO\\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();

输出:

<!DOCTYPE html><html xmlns="http://www.w3.org/1999/xhtml"><head>
  <meta name="viewport" content="width=device-width, initial-scale=1" />
  <link rel="shortcut icon" href="/icons/favicon-red.ico" />
  <link rel="apple-touch-icon" href="/icons/icon-red.png" />
  <title>self-signed.badssl.com</title>
  <link rel="stylesheet" href="/style.css" />
  <style>body { background: red; }</style>
</head>
<body>
<div id="content">
  <h1 style="font-size: 12vw;">
    self-signed.<br />badssl.com
  </h1>
</div>



</body></html>

注意: ,您必须添加一些导入:

import org.openqa.selenium.chrome.ChromeDriverService;
import org.openqa.selenium.chrome.ChromeOptions;
import org.openqa.selenium.remote.CapabilityType;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.openqa.selenium.remote.RemoteWebDriver;
import java.io.IOException;