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

来源:互联网 发布:如何查看网络ip地址 编辑:程序博客网 时间:2024/06/05 10:25

       本篇介绍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的值为Truecap.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 profileFirefoxProfile 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是可以在火狐上实现这个功能的。