Java+Selenium3方法篇32-处理不安全连接【转载】
本篇介绍webdriver处理不信任证书的情况,我们知道,有些网站打开是弹窗,SSL证书不可信任,但是你可以点击高级选项,继续打开不安全的链接。举例来说,大家都应该用过12306网站购票,点击新版购票,是不是会出现如下的界面。

先来看看chrome上如何处理这个,跳过图中这个步骤,直接到买票页面。
- package lessons;
- import org.openqa.selenium.WebDriver;
- import org.openqa.selenium.chrome.ChromeDriver;
- import org.openqa.selenium.remote.CapabilityType;
- import org.openqa.selenium.remote.DesiredCapabilities;
- public class HandPopup {
- public static void main(String[] args) throws Exception {
- // 创建DesiredCapabilities类的一个对象实例
- DesiredCapabilities cap=DesiredCapabilities.chrome();
- // 设置变量ACCEPT_SSL_CERTS的值为True
- cap.setCapability(CapabilityType.ACCEPT_SSL_CERTS, true);
- System.setProperty("webdriver.chrome.driver", ".\\Tools\\chromedriver.exe");
- // 打开带capability设置选项的浏览器
- WebDriver driver=new ChromeDriver(cap);
- driver.manage().window().maximize();
- driver.get("https://kyfw.12306.cn/otn");
- }
- }
- package lessons;
- import org.openqa.selenium.WebDriver;
- import org.openqa.selenium.firefox.FirefoxDriver;
- import org.openqa.selenium.firefox.FirefoxProfile;
- public class HandPopup {
- public static void main(String[] args) throws Exception {
- System.setProperty("webdriver.gecko.driver", ".\\Tools\\geckodriver.exe");
- // 创建 firefox profile
- FirefoxProfile profile = new FirefoxProfile();
- // 把这项值设置为True,就是接受不可信任的证书
- profile.setAcceptUntrustedCertificates(true);
- // 打开一个带上门设置好profile的火狐浏览器
- WebDriver driver = new FirefoxDriver(profile);
- driver.manage().window().maximize();
- driver.get("https://kyfw.12306.cn/otn");
- }
- }
想对作者说点什么? 我来说一句
zhangweitz2017-09-07 15:49:26#1楼DesiredCapabilities类也能支持firefox,用chrome同样的方法也可以在firefox上跑起来,郁闷的是get以后会被重定向到http://www.12306.cn/mormhweb/logFiles/error.html,不知道为什么
浙公网安备 33010602011771号