Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Handling 'choose a digital certificate' with selenium webdriver chrome

When I open a page with selenium webdriver I get a chrome dialog box "Choose certificate". The default certificate is the right one so I only have to click OK button. But I have problem with this. I have python code:

drv = webdriver.Chrome()   
drv.get("https://example.com/login")

and after that I want to do something like:

drv.switch_to_alert().accept()

or

drv.switch_to_alert().send_keys(Keys.TAB)
drv.switch_to_alert().send_keys(Keys.SPACE)

The problem is that the code stops executing on line drv.get("https://example.com/login"). Webdriver is waiting for page to load. And before that line there is no chrome dialog box.

How can I handle this?

like image 432
tstempko Avatar asked Feb 19 '13 14:02

tstempko


People also ask

How will you handle certificates using selenium?

Mastering XPath and CSS Selector for Selenium We can have certificates like the SSL certificate and insecure certificate. All these can be handled with the help of the DesiredCapabilities and ChromeOptions class. We shall create an object of the DesiredCapabilities class and apply setCapability method on it.

How does selenium handle SSL certificate in Chrome?

How does Selenium Webdriver handle SSL certificate in Chrome? For the Chrome browser, one can handle the SSL certificates using ChromeOptions class provided by Selenium WebDriver. It is a class that we can use to set properties for the Chrome browser.

How does selenium handle untrusted certificates?

Handle Untrusted Certificate SeleniumStep 1-We have to create FirefoxProfile in Selenium. Step 2- We have some predefined method in Selenium called setAcceptUntrustedCertificates() which accept Boolean values(true/false)- so we will make it true. Step 3-Open Firefox browser with the above-created profile.


1 Answers

You can configure you Policys Group for Chrome choose your certificate.

Chrome use a registry with a json with information of your certificate. After you configure gpo,find a way to modified this json with Python.

With C# I use Microsoft.Win32.Registry to manipulates the registrys.

Follow the steps to configure you gpo:

First I need to thanks IngussNeilands for the tutorial provided on his Github. It saved me! You can follow the steps on IngussNeilands´s tutorial here or follow my version of his tutorial below. ## Steps to Configure the Policy Groups
  1. Download Chrome Policy Tamplates from here: http://dl.google.com/dl/edgedl/chrome/policy/policy_templates.zip

  2. Extract the .zip file and find the chrome.adm that matches the country and the language settings on your Windows, following the path: policy_templates\windows\adm\<YourCountryAndLanguage>\chrome.adm

  3. Type "run" into your Windows Search Bar or press Windows + R. Then type de command gpedit.msc to open the The Local Group Policy Editor

  4. Now, access: 'Computer Policy>> Computer Configuration' and right-click the file 'Administrative Templates' and select 'Add or remove tamplates'

  5. Click 'add' and navigate to the chrome.adm that you choose before on 'policy_templates\windows\adm\<YourCountryAndLanguage>\chrome.adm'. Click to open it

  6. Now, navigate to: 'Computer Policy>> Computer Configuration>> Administrative Templates>> Classic Administrative Templates(ADM)>> Google>> Google Chrome>> Content Settings'

  7. Then on the rigth side of the window find and double-click the option 'Automatically select client certificates for these sites'

  8. Click the 'Enabled' option

  9. Now, Click the 'Show...' in the option pane below

  10. Copy and paste the 'JSON' below in the line of the column Value: {"pattern":"https://[*.]example.com","filter":{"ISSUER":{"CN":"example.com"}, "SUBJECT":{"CN":"value"}}. This JSON needs to be rewriten with your certificate informations

How to rewrite the Chrome Configure JSON

Ok, now a will give you a brief explanation on how to rewrite the Chrome Config JSON.

In the "pattern" key the value needs to be the URL that the certificate will be sent to. In most cases this URL is the same URL of the page, but some sites don´t use the same URL base to send the certificate. For example, when I was trying to webscraping the NFS-e in Uberlândia city I needed to debug the script of the page to find the URL to where the certificate was sent.

The "filter" key will have the certificate information. In my case, I need to access the same website with diferent certificates, for that I'll have to fill the JSON with the information of "ISSUER" and "SUBJECT". Chrome will choose one certificate that matches with the informations content in the filter key. For example, if I fill the "CN" from "ISSUER" object with "SERASA Certificadora Digital v5" I'll have more than one certificate with these informations and Chrome won´t be able to choose the right certificate.

In my git in here you can find the solution to alter the JSON for access the same site with more the one certificate.

like image 141
RodrigoSilvaPeres Avatar answered Sep 17 '22 11:09

RodrigoSilvaPeres