如果您想尝试使用注册表编辑器并使用 ChromeDriver,请尝试:
1-) 按 Windwos + R 并输入“regedit”。
2-) Rigth 点击“Computer\HKEY_LOCAL_MACHINE\SOFTWARE\Policies”,选择新建 -> 密钥并输入名称“Google”,然后在此密钥中添加密钥“Chrome”并在此添加名为“ AutoSelectCertificateForUrls”。完整路径必须是“Computer\HKEY_LOCAL_MACHINE\SOFTWARE\Policies\Google\Chrome\AutoSelectCertificateForUrls”。
3-) 右击 AutoSelectCertificateForUrls,新建 -> 字符串值,并输入名称“1”。
4-) 右击“1”,修改后复制粘贴这个 JSON “{”pattern”:”https://urlFromTheSiteYouWantAccess”,”filter”:{“ISSUER”:{“CN”:“你的cn"},"SUBJECT":{"CN":"你的证书 cn"}}}".
我编辑答案是因为我犯了一个错误,在这里放了一个 Java 的例子。对不起那个 Uldana Duisenaly。
但现在我将输入一个代码用于 python 更改最终访问一个带有 python 和 selenium 的网站。
不要忘记以管理员身份运行应用程序。
进口。
from os import times
import winreg
from OpenSSL import crypto
from selenium import webdriver
开证方法:
def GetCertificate(pathOfCertificate, passwordOfCertifcate):
pkcs12 = crypto.load_pkcs12(open(pathOfCertificate, 'rb').read(), passwordOfCertifcate)
return pkcs12.get_certificate()
更新注册表键“AutoSelectCertificateForUrls”中StringValue中Json的方法:
def UpdateStringValue(strigValueName,newValueOfStrinValue, stringValuePath):
key = winreg.OpenKey(winreg.HKEY_LOCAL_MACHINE, stringValuePath, 0, winreg.KEY_ALL_ACCESS)
winreg.SetValueEx(key, strigValueName, 0, winreg.REG_SZ, newValueOfStrinValue)
winreg.CloseKey(key)
我为路径声明 2 个变量的主要部分是字符串值和字符串值的名称。在我的情况下是“1”:
pathOfstringValue = 'SOFTWARE\Policies\Google\Chrome\AutoSelectCertificateForUrls'
stringValueName = '1'
获取证书及 Issuer 和 Subject 信息:
certificate = GetCertificate("C:\yourCrtificatePath\\Certificate.pfx", 'certificatePassword')
subject = certificate.get_subject()
issuer = certificate.get_issuer()
第一个 URL 是发送证书的地方,另一个是我们有证书登录按钮的页面:
url_where_certificate_will_be_send = "https://notacarioca.rio.gov.br/"
url = 'https://notacarioca.rio.gov.br/senhaweb/login.aspx'
构建json,(对不起,处理字符串的方式很丑)并调用方法来编辑字符串值注册表:
json = '{"pattern":"' + url_where_certificate_will_be_send + '","filter":{"ISSUER":{"CN":"' + issuer.CN + '","C":"' + issuer.C + '","O":"' + issuer.O + '"},"SUBJECT":{"CN":"' + subject.CN + '","C":"' + subject.C + '","O":"' + subject.O + '"}}}'
UpdateStringValue(stringValueName, json, pathOfstringValue)
最后是点击证书登录按钮的代码:
driver = webdriver.Chrome()
driver.get(url)
btn_certificate_login = driver.find_element_by_id('ctl00_cphCabMenu_imgLoginICP')
btn_certificate_login.click()
times.sleep(2.4)
driver.quit()
要下载代码,请访问我的 github here