Java+Selenium3方法篇32-处理不安全连接【转载】

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

 

先来看看chrome上如何处理这个,跳过图中这个步骤,直接到买票页面。

[java] view plain copy
  1. package lessons;  
  2.   
  3. import org.openqa.selenium.WebDriver;  
  4. import org.openqa.selenium.chrome.ChromeDriver;  
  5. import org.openqa.selenium.remote.CapabilityType;  
  6. import org.openqa.selenium.remote.DesiredCapabilities;  
  7.    
  8. public class HandPopup {  
  9.    
  10.     public static void main(String[] args) throws Exception {  
  11.           
  12.          // 创建DesiredCapabilities类的一个对象实例  
  13.         DesiredCapabilities cap=DesiredCapabilities.chrome();  
  14.            
  15.         // 设置变量ACCEPT_SSL_CERTS的值为True  
  16.         cap.setCapability(CapabilityType.ACCEPT_SSL_CERTS, true);  
  17.            
  18.         System.setProperty("webdriver.chrome.driver", ".\\Tools\\chromedriver.exe");   
  19.            
  20.         // 打开带capability设置选项的浏览器  
  21.         WebDriver driver=new ChromeDriver(cap);  
  22.         driver.manage().window().maximize();  
  23.         driver.get("https://kyfw.12306.cn/otn");  
  24.           
  25.     }  
  26.    
  27. }  
然后,我们来看看firefox上如何实现这个过程。
[java] view plain copy
  1. package lessons;  
  2.   
  3. import org.openqa.selenium.WebDriver;  
  4. import org.openqa.selenium.firefox.FirefoxDriver;  
  5. import org.openqa.selenium.firefox.FirefoxProfile;  
  6.    
  7. public class HandPopup {  
  8.    
  9.     public static void main(String[] args) throws Exception {  
  10.           
  11.         System.setProperty("webdriver.gecko.driver", ".\\Tools\\geckodriver.exe");    
  12.            
  13.         // 创建 firefox profile  
  14.         FirefoxProfile profile = new FirefoxProfile();  
  15.            
  16.         // 把这项值设置为True,就是接受不可信任的证书  
  17.         profile.setAcceptUntrustedCertificates(true);  
  18.            
  19.         // 打开一个带上门设置好profile的火狐浏览器  
  20.         WebDriver driver = new FirefoxDriver(profile);  
  21.           
  22.         driver.manage().window().maximize();  
  23.         driver.get("https://kyfw.12306.cn/otn");  
  24.           
  25.     }  
  26.    
  27. }  
运行发现,chrome的可以实现目的,firefox上却不可以。我也不知道什么鬼东西,只能推到geckodriver.exe的bug,因为之前selenium2.x是可以在火狐上实现这个功能的。

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/u011541946/article/details/74013466
想对作者说点什么? 我来说一句
  • zhangweitz
    DesiredCapabilities类也能支持firefox,用chrome同样的方法也可以在firefox上跑起来,郁闷的是get以后会被重定向到http://www.12306.cn/mormhweb/logFiles/error.html,不知道为什么

posted on 2018-05-02 11:24  okeymen  阅读(165)  评论(0)    收藏  举报

导航