您的位置:首页 > 编程语言 > Java开发

Java+Selenium3方法篇32-处理不安全连接

2017-06-30 22:51 351 查看
       本篇介绍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");

}

}
然后,我们来看看firefox上如何实现这个过程。
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");

}

}
运行发现,chrome的可以实现目的,firefox上却不可以。我也不知道什么鬼东西,只能推到geckodriver.exe的bug,因为之前selenium2.x是可以在火狐上实现这个功能的。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: