How to select Security Certificate From security Dialog



  • The problem I would like to solve is as follow:

    1. go to a website which is protected by client certificate authentication using browser
    2. once the security challenge dialog box is shown, select on the dialog
    3. look through the list of certificates I have in my keystore
    4. select the desired certificate and click 'ok'

    Is this possible with Selenium/Webdriver?

    enter image description here



  • I have the same issue. Currently my workaround is to use AutoIt. Below is the code that select desired certificate:

    public class CertificateDialogHandler
        {
                AutoItX3 autoIt = new AutoItX3(); 
    
            public void ChooseCertificate(string CertificateName, string windowTitle)
            {
                Thread.Sleep(2);
                autoIt.WinWaitActive(windowTitle, "", 0);
                string CurrentCertificate = "";
                ViewCertificateInfo(windowTitle);
                CurrentCertificate = GetCertificateIssuedTo();
    
                while (true)
                {
                    if (!CurrentCertificate.Equals(CertificateName))
                    {
                        SelectNextCertificate(windowTitle);
                        ViewCertificateInfo(windowTitle);
                        string newCert = GetCertificateIssuedTo();
                        if (newCert.Equals(CurrentCertificate))
                            throw new SystemException("Certificate was not found!");
                        else
                        {
                            CurrentCertificate = newCert;
                            continue;
                        }
                    }
    
                    else
                    {
                        PressBtnOK(windowTitle);
                        break;
                    }
                }
            }
    
            public void ConfirmSecurtyException()
            {
                Thread.Sleep(2);
                autoIt.WinWaitActive("Security Alert", "", 0);
    
                PressBtnYes();
    
            }
    
    
            public void CancelCertificateDialog(string windowTitle)
            {
                Thread.Sleep(2);
                autoIt.WinWaitActive(windowTitle, "", 0);
    
                PressBtnCancel();
    
            }
    
            /// 
            /// Open certificate details window
            /// 
            /// Title of certificate dialog
    
            private void ViewCertificateInfo(string windowTitle)
            {
                if (windowTitle.Equals("Choose a digital certificate"))
                {
                    autoIt.Send("{TAB}", 0);
                    Thread.Sleep(1000);
                }
    
                autoIt.Send("{SPACE}", 0);
                Thread.Sleep(1000);
                autoIt.WinWaitActive("Certificate", "", 0);
            }
    
            /// 
            /// Extract the certificate name from a "Certificate" window text
            /// 
            /// The certificate name
            private string GetCertificateIssuedTo()
            {
                string CertificateWndText;
                string sPattern;
                string[] SplittedCertificateWndText;
    
                CertificateWndText = autoIt.WinGetText("Certificate", "");
                sPattern = "General\n\n(.+?)\n";
                SplittedCertificateWndText = Regex.Split(CertificateWndText, sPattern);
    
                return SplittedCertificateWndText[1]; ;
            }
    
            /// 
            /// Determines whether the certificate name is founded 
            /// in the "Certificate" window text
            /// 
            /// 
            private bool IsFoundedCertificate(string CertificateName)
            {
                return GetCertificateIssuedTo() == CertificateName;
            }
    
            /// 
            /// Return to the "Choose a digital certificate" window
            /// and press the button "OK" when the certificate is founded
            private void PressBtnOK(string windowTitle)
            {
                if (!windowTitle.Equals("Choose a digital certificate"))
                {
                    autoIt.WinWaitActive("Certificate Details", "", 0);
                }
    
                Thread.Sleep(1000);
                autoIt.Send("{ESC}", 0);
    
                Thread.Sleep(1000);
                autoIt.Send("{TAB}", 0);
                Thread.Sleep(1000);
                autoIt.Send("{ENTER}", 0);
    
            }
    
            private void PressBtnCancel()
            {
                //Thread.Sleep(1000);
                //AutoIt.Send("{ESC}", 0);
                for (int i = 0; i < 3; i++)
                {
                    autoIt.Send("{TAB}", 0);
                    Thread.Sleep(1000);
                }
                autoIt.Send("{SPACE}", 0);
                Thread.Sleep(1000);
            }
            private void PressBtnYes()
            {
                //Thread.Sleep(1000);
                //AutoIt.Send("{ESC}", 0);          
                for (int i = 0; i < 2; i++)
                {
                    autoIt.Send("{TAB}", 0);
                    Thread.Sleep(1000);
                }
                autoIt.Send("{SPACE}", 0);
                Thread.Sleep(1000);
            }
    
            /// 
            /// Return to the "Choose a digital certificate" window and 
            /// select next certificate name from the name list
            /// 
            private void SelectNextCertificate(string windowTitle)
            {
                if (!windowTitle.Equals("Choose a digital certificate"))
                {
                    autoIt.WinWaitActive("Certificate Details", "", 0);
                }
    
                Thread.Sleep(1000);
                autoIt.Send("{ESC}", 0);
    
    
                Thread.Sleep(1000);
                autoIt.Send("{TAB}", 0);
                Thread.Sleep(1000);
                autoIt.Send("{DOWN}", 0);
            }
        }
    


Suggested Topics

  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2