1. 程式人生 > >Java+Selenium3方法篇32-處理不安全連線

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");
		
	}
 
}
然後,我們來看看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是可以在火狐上實現這個功能的。