15

I am automating tests using selenium chromewebdriver 3.7. Whenever I lauch the site, I get a certificate selection popup like the one belowenter image description here

However I am not able to click on the OK button. These are the options I have tried

 //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();

All my trials have failed. How can I click on OK on this popup window? This is the closest solution I found which is not working Link here

user4237435
  • 333
  • 1
  • 2
  • 12

5 Answers5

8

I also had problems with accepting the warning for using a signed certificate. The solution of @eskoba worked like a charm. The functions are NOT final, because I let the enter button press for 10 times. I made this, because the webdriver needs a long time until it actually calls the url. In the meantime he starts pressing already.

In 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()
chainstair
  • 681
  • 8
  • 18
  • 1
    Great answer! For a clearer definite, some important import goes like: import threading import pyautogui then define a browser i.e. browser = webdriver.Chrome(executable_path = 'chromedriver.exe PATH'); then you can put above code into work. One more comment here is that you can put a time.sleep(10) in the threaded_function2 so that it would allow 10 seconds for example to let the url load. – Gin Dec 19 '19 at 03:25
5

If still actual, I had same issue on Mac, and solution was simple:

  • for chrome is set AutoSelectCertificateForUrls policy like that:

    defaults write com.google.Chrome AutoSelectCertificateForUrls -array-add -string '{"pattern":"[*.]example.com","filter":{"ISSUER":{"CN":"**cert issuer**"}, "SUBJECT":{"CN": "**cert name**"}}}'
    
  • for safari:

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

then use it in code like subprocess.call() in python

Saeid Amini
  • 1,313
  • 5
  • 16
  • 26
Bidonus
  • 61
  • 1
  • 4
  • The policy can be enabled also via registry and other approaches in multiple operating systems. ``` See the policy docs https://www.chromium.org/administrators/policy-list-3#AutoSelectCertificateForUrls – Alexey Kuptsov Aug 15 '20 at 19:12
  • This is how I configured this in a `.reg` file: ``` [HKEY_CURRENT_USER\SOFTWARE\Policies\Google\Chrome\AutoSelectCertificateForUrls] "1"="{\"pattern\":\"https://example.com\",\"filter\":{\"ISSUER\":{\"CN\":\"example.com\"}, \"SUBJECT\":{\"CN\":\"Alex McSubjectUser\"}}}" ``` – Alexey Kuptsov Aug 15 '20 at 19:21
1

I had the same problem and I was able to solve it by using the robot, creating function for the url and passing it to a different thread.

    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();
       }
eskoba
  • 532
  • 1
  • 7
  • 25
  • 1
    Can you explain, flow of this call ? – deepak Jul 30 '19 at 14:40
  • Where does the `robot` come from? I assume you mean the [Selenium Robot class](https://www.guru99.com/using-robot-api-selenium.html)? – Jonathan Benn Oct 31 '19 at 13:34
  • Oups, the `Robot` class comes from AWT – Jonathan Benn Oct 31 '19 at 13:40
  • can you please explain this? – forkdbloke May 19 '20 at 18:58
  • 1
    It seems that the answer `AutoSelectCertificateForUrls` is more actual at the moment. – Alexey Kuptsov Aug 15 '20 at 19:24
  • `AutoSelectCertificateForUrls` seems to be not a good option if we want to use different certificates in our tests. Maybe changing values of `AutoSelectCertificateForUrls` dynamically during tests, before running browser, could be a workaround? However, I am afraid that it could require higher user permissions to edit registry. – matandked Sep 28 '20 at 13:20
  • Have followed the same approach, worked for windows based chrome but failed on my docker chrome image. Is there's anything different needs to be done? – RISHI KHANNA Feb 09 '23 at 15:44
0

One suggestion would be, use Sikuli to click on OK button in the certificate.

Steps:

  1. Take screenshot of OK button and save it.
  2. Download sikuli-script.jar and add it to Project's Build path.
  3. Take a screenshot of the UI Element to be clicked and save it locally.
  4. Add the following code to the test case.

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

Other functions Sikuli provides can be found here.

Prathibha
  • 199
  • 13
-2

You can also skip being prompted when a certificate is missing, invalid, or self-signed.

You would need to set acceptInsecureCerts in DesiredCapabilities and pass that when you create a driver instance.

for example, in 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)
Corey Goldberg
  • 59,062
  • 28
  • 129
  • 143
  • 1
    Your solution is not correct, because it is not referred to this specific problem. You want to bypass the insecure certificate warning from your browser, but this what @user4237435 asked for was to bypass the warning to actually use a signed certificate. With your solution you get rid of that only: https://www.veterinaryha.org/images/2018/Blog/June/non-secure-http-warning-message.png – chainstair Feb 21 '19 at 18:49