使用Selenium webDriver检查网站是否有任何ssl证书警告 [英] Check if website has any ssl certificate warnings using Selenium webDriver

查看:197
本文介绍了使用Selenium webDriver检查网站是否有任何ssl证书警告的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要自动化一个场景,在该场景中,我必须确认网站没有关于ssl证书的警告.如何使用Selenium WebDriver 2对其进行存档?

I need to automate a scenario where I have to verify that website has no warnings regarding ssl certificates. How to archive it using Selenium WebDriver 2?

推荐答案

SSL证书警告在每种浏览器上似乎有所不同(因为它们是由浏览器本身生成的,而不是由您尝试访问的网页生成的.)

SSL certificate warnings appear different on each browser (as they are generated by the browser itself and not by the web-page that you are trying to access).

如果您在Firefox中使用Selenium,则可以使用以下Java类:

If you are using Selenium with Firefox, then you can use the following Java class:

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.firefox.FirefoxBinary;

class Whatever
{
    private WebDriver webDriver;

    public void open() throws Exception
    {
        webDriver = new FirefoxDriver();
    }

    public void openHeadless() throws Exception
    {
        FirefoxBinary binary = new FirefoxBinary(new File("/usr/local/bin/firefox"));
        binary.setEnvironmentProperty("DISPLAY",System.getProperty("lmportal.xvfb.id",":99"));
        webDriver = new FirefoxDriver(binary,null);
    }

    public void close() throws Exception
    {
        webDriver.quit();
    }

    public boolean sslWarningIssued(final String url) throws Exception
    {
        webDriver.get(url);
        String pageSource = webDriver.getPageSource();
        return webDriver.getTitle().equals("Untrusted Connection"); // should be faster than the one below...
        return pageSource.contains("This Connection is Untrusted") && pageSource.contains("What Should I Do?");
    }
}

然后您可以致电:

Whatever instance = new Whatever();
instance.open(); // or openHeadless for faster execution
boolean sslWarningIssued1 = instance.sslWarningIssued(website_1_url);
boolean sslWarningIssued2 = instance.sslWarningIssued(website_2_url);
...
boolean sslWarningIssuedN = instance.sslWarningIssued(website_N_url);
instance.close();

这篇关于使用Selenium webDriver检查网站是否有任何ssl证书警告的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆