《手把手教你》系列技巧篇(四十三)-java+ selenium自动化测试-处理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的参考代码

posted @   北京-宏哥  阅读(675)  评论(0编辑  收藏  举报
编辑推荐:
· .NET Core 托管堆内存泄露/CPU异常的常见思路
· PostgreSQL 和 SQL Server 在统计信息维护中的关键差异
· C++代码改造为UTF-8编码问题的总结
· DeepSeek 解答了困扰我五年的技术问题
· 为什么说在企业级应用开发中,后端往往是效率杀手?
阅读排行:
· 10亿数据,如何做迁移?
· 推荐几款开源且免费的 .NET MAUI 组件库
· 清华大学推出第四讲使用 DeepSeek + DeepResearch 让科研像聊天一样简单!
· c# 半导体/led行业 晶圆片WaferMap实现 map图实现入门篇
· 易语言 —— 开山篇
停止精灵球

喜欢请打赏

扫描二维码打赏

了解更多

点击右上角即可分享
微信分享提示