Selenium Webdriver 如何在 Firefox 中处理 SSL 证书?

seleniumautomation testingtesting tools

我们可以借助 Selenium Webdriver 使用 FirefoxProfile 类在 Firefox 中处理 SSL 证书。然后将参数 setAcceptUntrustedCertificates 设置为 true。SSL 是一种在客户端(浏览器)和服务器之间建立安全连接的协议。

SSL 会检查网站的真实性,并在访问者发送或从网站获取信息时对其进行加密。 SSL 证书的一些优势如下:

  • 通过促进业务增长赢得用户信任。

  • 通过保护用户名、密码和其他银行信息等客户数据,为在线支付提供安全的网关。

  • 远离黑客威胁。

  • 提升品牌价值,例如,Google 会为拥有有效 SSL 的网站提供良好的排名。

SSL 证书网站以 https:// 开头,如果连接安全,我们可以在地址栏中找到绿色的锁形图标。

语法

FirefoxProfile p=new FirefoxProfile();
p.setAcceptUntrustedCertificates(true);

示例

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.firefox.FirefoxProfile;
public class SSLErrorFirefox{
   public static void main(String[] args) {
      //DesiredCapabilities 对象
      DesiredCapabilities c=DesiredCapabilities.internetExplorer();
      //设置 FirefoxProfile
      FirefoxProfile p=new FirefoxProfile();
      // 将 setAcceptUntrustedCertificates 设置为 true
      p.setAcceptUntrustedCertificates(true);
      System.setProperty(&"webdriver.gecko.driver", &"C:\Users\ghs6kor\Desktop\Java\geckodriver.exe");
      //将此配置文件配置到浏览器
      WebDriver driver=new FirefoxDriver(p);
      //隐式等待
      driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);
      //URL 启动
      driver.get("application url to be entered");
   }
}

相关文章