【问题标题】:selenium chrome driver select certificate popup confirmation not workingselenium chrome 驱动程序选择证书弹出确认不起作用
【发布时间】:2018-09-05 19:31:44
【问题描述】:

我正在使用 selenium chromewebdriver 3.7 自动化测试。每当我启动该站点时,我都会弹出一个证书选择弹出窗口,如下所示

但是我无法单击“确定”按钮。这些是我尝试过的选项

 //I have tried getWindowHandle like this  
 String  handle= driver.getWindowHandle();
        this.driver.switchTo().window(handle);

//I have alos tried switching and accept
 driver.switchTo().alert().accept();

//I have also tried to force the enter key like this
 robot.keyPress(KeyEvent.VK_ENTER);
 robot.keyRelease(KeyEvent.VK_ENTER);

 // I also tried this way
 Scanner keyboard = new Scanner(System.in);
 keyboard.nextLine();

我所有的试验都失败了。如何在此弹出窗口上单击“确定”? 这是我发现的最接近的解决方案,但不起作用Link here

【问题讨论】:

    标签: java selenium testing selenium-webdriver automated-tests


    【解决方案1】:

    一个建议是,使用 Sikuli 点击证书中的确定按钮。

    步骤:

    1. 截取确定按钮并保存。
    2. 下载 sikuli-script.jar 并将其添加到项目的构建路径中。
    3. 截取要点击的 UI 元素并保存到本地。
    4. 将以下代码添加到测试用例中。

      Screen s=new Screen(); s.click(“image name”);

    Sikuli 提供的其他功能可以在here 找到。

    【讨论】:

      【解决方案2】:

      当证书丢失、无效或自签名时,您也可以跳过提示。

      您需要在DesiredCapabilities 中设置acceptInsecureCerts 并在创建驱动程序实例时传递它。

      例如,在 Python 中:

      from selenium import webdriver
      from selenium.webdriver.common.desired_capabilities import DesiredCapabilities
      
      caps = DesiredCapabilities.CHROME.copy()
      caps['acceptInsecureCerts'] = True
      driver = webdriver.Chrome(desired_capabilities=caps)
      

      【讨论】:

      • 你的解决方法不正确,因为没有提到这个具体问题。您想绕过浏览器的不安全证书警告,但@user4237435 要求的是绕过警告以实际使用签名证书。使用您的解决方案,您只需摆脱它:veterinaryha.org/images/2018/Blog/June/…
      【解决方案3】:

      我遇到了同样的问题,我能够通过使用机器人来解决它,为 url 创建函数并将其传递给不同的线程。

          Runnable mlauncher = () -> {
          try {
      
            driver.get(url);
           } catch (Exception e) {
                e.printStackTrace();
             }
          };
      
      public void myfunction {
       try {
      
         Thread mthread = new Thread(mlauncher);
         mthread.start
      
        robot.keyPress(KeyEvent.VK_ENTER);
        robot.keyRelease(KeyEvent.VK_ENTER);
      
       } catch (Exception e) {
                e.printStackTrace();
             }
      

      【讨论】:

      • 你能解释一下这个电话的流程吗?
      • robot 来自哪里?我假设你的意思是Selenium Robot class?
      • Oups,Robot 类来自 AWT
      • 你能解释一下吗?
      • 目前看来AutoSelectCertificateForUrls的答案比较实际。
      【解决方案4】:

      我在接受使用签名证书的警告时也遇到了问题。 @eskoba 的解决方案就像一个魅力。这些功能不是最终的,因为我让输入按钮按下了 10 次。我做了这个,因为 webdriver 需要很长时间才能真正调用 url。与此同时,他已经开始施压了。

      在 Python 中

      def threaded_function():
          #Calls the website
          browser.get(url)
      
      def threaded_function2():
          #Presses 10 times
          for i in range(0,10):
              pyautogui.press('enter')
      
      #Calling the website and pressing 10 times in the same time
      thread2 = Thread(target = threaded_function2)
      thread2.start()
      
      thread = Thread(target = threaded_function)
      thread.start()
      

      【讨论】:

      • 很好的答案!为了更明确,一些重要的导入如下: import threading import pyautogui 然后定义一个浏览器,即 browser = webdriver.Chrome(executable_path = 'chromedriver.exe PATH');然后你可以把上面的代码投入工作。这里还有一条评论是,您可以在 threaded_function2 中放置一个 time.sleep(10) 以便它允许 10 秒,例如让 url 加载。
      【解决方案5】:

      如果仍然存在,我在 Mac 上遇到了同样的问题,解决方案很简单:

      • 为 chrome 设置了 AutoSelectCertificateForUrls 这样的策略:

        defaults write com.google.Chrome AutoSelectCertificateForUrls -array-add -string '{"pattern":"[*.]example.com","filter":{"ISSUER":{"CN":"**cert issuer**"}, "SUBJECT":{"CN": "**cert name**"}}}'
        
      • 对于野生动物园:

        security set-identity-preference -c "**cert name**" -s "**example.com**"
        

      然后在代码中使用它 subprocess.call() 在 python 中

      【讨论】:

      • 该策略也可以通过注册表和其他方法在多个操作系统中启用。 ``` 查看政策文档chromium.org/administrators/…
      • 这就是我在.reg 文件中配置它的方式:``` [HKEY_CURRENT_USER\SOFTWARE\Policies\Google\Chrome\AutoSelectCertificateForUrls] "1"="{\"pattern\":\ "example.com\",\"filter\":{\"ISSUER\":{\"CN\":\"example.com\"},\"SUBJECT\":{\"CN\":\ “亚历克斯 McSubjectUser\"}}}" ```
      最近更新 更多