《手把手教你》系列技巧篇(四十三)-java+ selenium自动化测试-处理https 安全问题或者非信任站点-上篇(详解教程)

简介: 【5月更文挑战第7天】本文介绍了如何在Java+Selenium自动化测试中处理浏览器对不信任证书的处理方法,特别是针对IE、Chrome和Firefox浏览器。在某些情况下,访问HTTPS网站时会遇到证书不可信的警告,但可以通过编程方式跳过这些警告。

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的参考代码




每天学习一点,今后必成大神-

往期推荐(由于跳转参数丢失了,所有建议选中要访问的右键,在新标签页中打开链接即可访问):


Appium自动化系列,耗时80天打造的从搭建环境到实际应用精品教程测试

Python接口自动化测试教程,熬夜87天整理出这一份上万字的超全学习指南

Python+Selenium自动化系列,通宵700天从无到有搭建一个自动化测试框架

Java+Selenium自动化系列,仿照Python趁热打铁呕心沥血317天搭建价值好几K的自动化测试框架

Jmeter工具从基础->进阶->高级,费时2年多整理出这一份全网超详细的入门到精通教程

Fiddler工具从基础->进阶->高级,费时100多天吐血整理出这一份全网超详细的入门到精通教程

Pycharm工具基础使用教程

相关文章
|
2天前
|
存储 安全 测试技术
《手把手教你》系列技巧篇(六十三)-java+ selenium自动化测试 - cookie -上篇(详细教程)
【6月更文挑战第4天】本文介绍了Cookie和Session的概念及其用途。Cookie是服务器发送到浏览器并存储在本地的小型文本文件,用于记录用户信息,如登录状态。它分为会话Cookie(关闭浏览器即消失)和永久Cookie(设置过期时间)。Session则是在服务器端保存用户状态的一种方式,比Cookie更安全,但会占用服务器资源。Selenium提供了操作Cookie的API,包括添加、删除和获取Cookie。文章还提到了Cookie的优缺点,如大小限制和潜在的安全风险。
8 1
《手把手教你》系列技巧篇(六十三)-java+ selenium自动化测试 - cookie -上篇(详细教程)
|
3天前
|
Java 程序员
33. 【Java教程】泛型
33. 【Java教程】泛型
12 0
|
3天前
|
存储 IDE Java
32. 【Java教程】集合
32. 【Java教程】集合
12 1
|
3天前
|
Java 编译器
31. 【Java教程】枚举类
31. 【Java教程】枚举类
8 1
|
3天前
|
Java C语言 开发者
30. 【Java教程】包装类
30. 【Java教程】包装类
8 0
|
3天前
|
SQL Java 编译器
29. 【Java教程】异常处理
29. 【Java教程】异常处理
14 3
|
3天前
|
小程序 Java
28. 【Java教程】Scanner 类
28. 【Java教程】Scanner 类
18 7
|
3天前
|
存储 安全 IDE
27. 【Java教程】StringBuilder
27. 【Java教程】StringBuilder
21 1
|
3天前
|
Java 索引
26. 【Java教程】 String类
26. 【Java教程】 String类
15 0
|
Java 数据库 容器