1.簡介
這一篇宏哥主要介紹webdriver在IE、Chrome和Firefox三個瀏覽器上處理不信任證書的情況,我們知道,有些網站打開是彈窗,SSL證書不可信任,但是你可以點擊高級選項,繼續打開不安全的鏈接。舉例來說,想必大家都應該用過前幾年的12306網站購票,點擊新版購票,是不是會出現如下的界面。宏哥又找了一個https的頁面,如下圖所示:

2.三種瀏覽器如何處理不受信任的證書
三種瀏覽器訪問網頁,彈出證書不信任,需要點擊下信任繼續訪問才行,多為訪問https的網頁。那么我們在做自動化測試的時候,如何跳過這一步驟,直接訪問到我們需要的頁面了,這個就是宏哥主要分享和講解的如何在三大瀏覽器跳過這一步驟。
3.IE瀏覽器
3.1代碼設計

3.2參考代碼
package lessons; import org.openqa.selenium.WebDriver; import org.openqa.selenium.ie.InternetExplorerDriver; /** * @author 北京-宏哥 * *《手把手教你》系列技巧篇(四十三)-java+ selenium自動化測試-處理https 安全問題或者非信任站點-上篇(詳解教程) * * 2021年11月11日 */ public class TestHttps { public static void main(String[] args) throws Exception { // Set the driver path System.setProperty("webdriver.ie.driver", ".\\Tools\\IEDriverServer.exe"); WebDriver driver = new InternetExplorerDriver(); Thread.sleep(1000); driver.manage().window().maximize(); driver.get("https://www.21xrx.com/"); Thread.sleep(2000); String js = "javascript:document.getElementById('overridelink').click();"; //To click on "Continue to this website (not recommended)." link to load original website. //driver.navigate().to("javascript:document.getElementById('overridelink').click()"); driver.get(js); //JavascriptExecutor jsExecutor = (JavascriptExecutor) driver; //jsExecutor.executeScript(js); System.out.println(" 嘿嘿!宏哥,你已經成功跳過證書信任步驟啦!"); } }
3.3運行代碼
1.運行代碼,右鍵Run AS->Java Appliance,控制台輸出,如下圖所示:

2.運行代碼后電腦端的瀏覽器的動作,如下小視頻所示:
4.Firefox瀏覽器
4.1代碼設計

4.2參考代碼
package lessons; import org.openqa.selenium.WebDriver; import org.openqa.selenium.firefox.FirefoxDriver; import org.openqa.selenium.firefox.FirefoxOptions; import org.openqa.selenium.firefox.FirefoxProfile; /** * @author 北京-宏哥 * *《手把手教你》系列技巧篇(四十三)-java+ selenium自動化測試-處理https 安全問題或者非信任站點(詳解教程) * * 2021年11月11日 */ public class TestHttps { 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); FirefoxOptions firefoxOptions = new FirefoxOptions(); firefoxOptions.setProfile(profile); // 打開一個帶上門設置好profile的火狐瀏覽器 WebDriver driver = new FirefoxDriver(firefoxOptions); //driver.setProfile(profile); driver.manage().window().maximize(); driver.get("https://www.21xrx.com/"); System.out.println(" 嘿嘿!宏哥,你已經成功跳過證書信任步驟啦!"); } }
4.3運行代碼
1.運行代碼,右鍵Run AS->Java Appliance,控制台輸出,如下圖所示:

2.運行代碼后電腦端的瀏覽器的動作,如下小視頻所示:
5.小結
5.1IE瀏覽器遇到問題及解決辦法
1.運行IE瀏覽器報錯:
Exception in thread "main" org.openqa.selenium.SessionNotCreatedException: Unexpected error launching Internet Explorer. Protected Mode settings are not the same for all zones. Enable Protected Mode must be set to the same value (enabled or disabled) for all zones.

解決辦法:
有的小伙伴或者童鞋們可能覺得是版本的問題,宏哥第一想法也是這個問題,但是又想了想,以前可以運行現在連瀏覽器的啟動不了,確定不是版本問題,而是由其他原因引起的。這是因為沒有關閉IE瀏覽器的保護模式。當運行測試用例后出現類似以下內容的錯誤:
Exception in thread "main" org.openqa.selenium.remote.SessionNotFoundException: Unexpected error launching Internet Explorer.
Protected Mode settings are not the same for all zones. Enable Protected Mode must be set to the same value (enabled or disabled) for all zones.
應該就是IE瀏覽器的保護模式未關閉。

在這里可以關閉保護模式。需要注意的是,我們訪問的站點是哪個區域的,就要把那個區域的保護模式觀點。(一般來說我都是關全部)
而針對IE10及以上版本,我們需要關閉“增強保護模式”

PS: 請注意這里的選項是“重啟計算機后生效”!而針對IE11,我們需要進一步修改注冊表。(Run->regedit->Enter)

如果FeatureControl下沒有FEATURE_BFCACHE,就以FEATURE_BFCACHE為名new一個key!並在其下創建一個DWORD,取名為:iexplore.exe,value值為0。
另外,別忘了一件事情,就是IE的縮放選項。請設置縮放選項為100%,否則可能無法定位頁面元素。

2.IE以前遇到這種問題代碼這么寫,就可以現在就不行了,所以宏哥換了一種方式,利用前邊學習過的JavaScript執行知識進行解決。
package lessons; import org.openqa.selenium.WebDriver; import org.openqa.selenium.firefox.FirefoxDriver; import org.openqa.selenium.firefox.FirefoxOptions; import org.openqa.selenium.firefox.FirefoxProfile; import org.openqa.selenium.ie.InternetExplorerDriver; import org.openqa.selenium.remote.CapabilityType; import org.openqa.selenium.remote.DesiredCapabilities; /** * @author 北京-宏哥 * *《手把手教你》系列技巧篇(四十三)-java+ selenium自動化測試-處理https 安全問題或者非信任站點(詳解教程) * * 2021年11月11日 */ public class TestHttps { public static void main(String[] args) throws Exception { // Create object of DesiredCapabilities class DesiredCapabilities cap=DesiredCapabilities.internetExplorer(); cap.setCapability(InternetExplorerDriver.INTRODUCE_FLAKINESS_BY_IGNORING_SECURITY_DOMAINS, true); cap.setCapability(CapabilityType.ACCEPT_SSL_CERTS, true); cap.setJavascriptEnabled(true); // Set the driver path System.setProperty("webdriver.ie.driver", ".\\Tools\\IEDriverServer.exe"); // Open browser with capability WebDriver driver=new InternetExplorerDriver(cap); driver.manage().window().maximize(); driver.get("https://www.21xrx.com/"); System.out.println(" 嘿嘿!宏哥,你已經成功跳過證書信任步驟啦!"); } }
3.也許有的小伙伴或者童鞋們,發現使用宏哥的代碼也不成功,那是因為你沒有將所有的安全保護模式關閉,解決辦法:參考宏哥知識點1,將所有安全保護模式關閉,再次運行代碼就成功了。
5.2Firefox瀏覽器遇到問題及解決辦法
1.Firefox以前遇到這種問題代碼這么寫,就可以現在就不行了,所以宏哥也換了一種方式。
package lessons; import org.openqa.selenium.WebDriver; import org.openqa.selenium.firefox.FirefoxDriver; import org.openqa.selenium.firefox.FirefoxOptions; import org.openqa.selenium.firefox.FirefoxProfile; import org.openqa.selenium.ie.InternetExplorerDriver; import org.openqa.selenium.remote.CapabilityType; import org.openqa.selenium.remote.DesiredCapabilities; /** * @author 北京-宏哥 * *《手把手教你》系列技巧篇(四十三)-java+ selenium自動化測試-處理https 安全問題或者非信任站點(詳解教程) * * 2021年11月11日 */ public class TestHttps { 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://www.21xrx.com/"); System.out.println(" 嘿嘿!宏哥,你已經成功跳過證書信任步驟啦!"); } }
但是代碼報錯如下圖所示:

解決辦法:宏哥換了一種寫法,查看4.2的參考代碼。
