在看selenium相关文档时,看到了selenium对不同浏览器处理SSL Certificate Error的方法。
因为还没遇到,所以暂时没试,只是记录下来了。
Firefox
- 创建一个新的profile,命名为certificateIssue
(创建方法参照:http://blog.csdn.net/qiyueqinglian/article/details/43053531) - 用刚才创建的profile打开ffx浏览器
- 访问报SSL certificate error的URL,通过点击 I Understnad the Risks -> Add Exception button,引进证书,这样就不报错了.
- 在代码里配置通过刚才创建的profile打开ffx
代码如下
ProfilesIni firProfiles = new ProfilesIni();
FirefoxProfile wbdrverprofile = firProfiles.getProfile("certificateIssue");
//在文章的开始我也说了,代码没做实验,这里有个疑问点。是不是设置了下面两行,其实就不用手动创建profile,并认证,然后手动引进证书。等遇到了这类网址再做实验吧。
wbdrverprofile.setAcceptUntrustedCertificates(true);
wbdrverprofile.setAssumeUntrustedCertificateIssuer(false);
WebDriver Driver = new FirefoxDriver(wbdrverprofile);
Driver.get("site URL where certificate error");
Chrome
//Set chrome browser's capabilities to to accept SSL certificate on runtime.
DesiredCapabilities capability = DesiredCapabilities.chrome();
capability.setCapability(CapabilityType.ACCEPT_SSL_CERTS, true);
System.setProperty("webdriver.chrome.driver", "D:\\chromedriver_win32\\chromedriver.exe");
WebDriver driver = new ChromeDriver(capability);
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
//Enter the URL of site where you facing SSL error.
driver.get("Enter site URL");
IE
IE需要通过js来解决。在你遇到证书错误的页面点击F12,就会看到继续访问该页面的ID是”overridelink”。
点击这个就可以继续访问了。
// Set path of IEDriverServer.exe
System.setProperty("webdriver.ie.driver", "D://IEDriverServer.exe");
WebDriver driver = new InternetExplorerDriver();
driver.manage().timeouts().implicitlyWait(15, TimeUnit.SECONDS);
driver.get("URL of SSL error site");
//To click on "Continue to this website (not recommended)." link to load original website.
driver.navigate().to("javascript:document.getElementById('overridelink').click()");