selenium headless chrome java忽略ssl错误

时间:2017-12-11 06:42:02

标签: java google-chrome selenium ssl-certificate selenium-chromedriver

使用unix中的java进行Selenium无头chrome测试会返回空页源

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

这是由于访问安全的http(https)网站。

有没有办法忽略ssl证书问题?请让我知道如何忽略它。

Selenium版本3.7.1..java版本1.8.0.144 chrome驱动程序版本2.33 Chrome版本62 +

我尝试了下面的选项..但它似乎没有用。

1. ChromeOptions options = new ChromeOptions();
            options.setAcceptInsecureCerts(true);
            options.setHeadless(true);DesiredCapabilities capabilities = 
   DesiredCapabilities.chrome();
                capabilities.setCapability("chrome.switches", Arrays.asList("--
                ignore-certificate-errors,--web-security=false,--ssl-
                protocol=any,--ignore-ssl-errors=true"));
   capabilities.setCapability(ChromeOptions.CAPABILITY, options);
   driver = new ChromeDriver(capabilities);

2. DesiredCapabilities capabilities = DesiredCapabilities.chrome();
        capabilities.setCapability (CapabilityType.ACCEPT_SSL_CERTS, true);
        capabilities.setCapability (CapabilityType.ACCEPT_INSECURE_CERTS, true);
        capabilities.setCapability(ChromeOptions.CAPABILITY, options);
   driver = new ChromeDriver(capabilities);

我是以正确的方式做这件事的吗?让我知道让它工作的诀窍

提前致谢

完整代码:

WebDriver driver = null;

        try {

            String filePath = "Path to driver";
            System.setProperty("webdriver.chrome.driver", filePath);

            ChromeOptions options = new ChromeOptions();
            options.addArguments("--headless");
            options.addArguments("--no-sandbox");
            options.setAcceptInsecureCerts(true);
            options.addArguments("test-type");

            String[] switches = {"--ignore-certificate-errors"};

            DesiredCapabilities capabilities = DesiredCapabilities.chrome();
            capabilities.setCapability("chrome.switches", Arrays.asList(switches));
            capabilities.setCapability (CapabilityType.ACCEPT_SSL_CERTS, true);
            capabilities.setCapability(ChromeOptions.CAPABILITY, options);

            driver = new ChromeDriver(capabilities);
            driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
            driver.get("https://meta.stackexchange.com");
            driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
            System.out.println("PAGE SOURCE : \n" + driver.getPageSource());

        } catch (Exception ex) {
            ex.printStackTrace();
        } finally {
            driver.close();
            driver.quit();
        }

1 个答案:

答案 0 :(得分:0)

现在您可以在选项中添加功能。请尝试以下操作:

        ChromeOptions options = new ChromeOptions();
        options.addArguments("--disable-dev-shm-usage");
        options.addArguments("--no-sandbox");
        options.addArguments("--headless", "--window-size=1920,1200", "--ignore-certificate-errors");
        options.setCapability(CapabilityType.ACCEPT_SSL_CERTS, true);
        options.setCapability(CapabilityType.ACCEPT_INSECURE_CERTS, true);
        driver = new ChromeDriver(options);