How to deal with certificates using Selenium?
Asked Answered
P

20

118

I am using Selenium to launch a browser. How can I deal with the webpages (URLs) that will ask the browser to accept a certificate or not?

In Firefox, I may have a website like that asks me to accept its certificate like this:

Firefox

On the Internet Explorer browser, I may get something like this:

Enter image description here

On Google Chrome:

Google Chrome

I repeat my question: How can I automate the acceptance of a website's certificate when I launch a browser (Internet Explorer, Firefox and Google Chrome) with Selenium (Python programming language)?

Piave answered 1/7, 2014 at 9:41 Comment(0)
L
194

For the Firefox, you need to set accept_untrusted_certs FirefoxProfile() option to True:

from selenium import webdriver

profile = webdriver.FirefoxProfile()
profile.accept_untrusted_certs = True

driver = webdriver.Firefox(firefox_profile=profile)
driver.get('https://cacert.org/')

driver.close()

For Chrome, you need to add --ignore-certificate-errors ChromeOptions() argument:

from selenium import webdriver

options = webdriver.ChromeOptions()
options.add_argument('ignore-certificate-errors')

driver = webdriver.Chrome(chrome_options=options)
driver.get('https://cacert.org/')

driver.close()

For the Internet Explorer, you need to set acceptSslCerts desired capability:

from selenium import webdriver

capabilities = webdriver.DesiredCapabilities().INTERNETEXPLORER
capabilities['acceptSslCerts'] = True

driver = webdriver.Ie(capabilities=capabilities)
driver.get('https://cacert.org/')

driver.close()

Actually, according to the Desired Capabilities documentation, setting acceptSslCerts capability to True should work for all browsers since it is a generic read/write capability:

acceptSslCerts

boolean

Whether the session should accept all SSL certs by default.


Working demo for Firefox:

>>> from selenium import webdriver

Setting acceptSslCerts to False:

>>> capabilities = webdriver.DesiredCapabilities().FIREFOX
>>> capabilities['acceptSslCerts'] = False
>>> driver = webdriver.Firefox(capabilities=capabilities)
>>> driver.get('https://cacert.org/')
>>> print(driver.title)
Untrusted Connection
>>> driver.close()

Setting acceptSslCerts to True:

>>> capabilities = webdriver.DesiredCapabilities().FIREFOX
>>> capabilities['acceptSslCerts'] = True
>>> driver = webdriver.Firefox(capabilities=capabilities)
>>> driver.get('https://cacert.org/')
>>> print(driver.title)
Welcome to CAcert.org
>>> driver.close()
Lombardi answered 31/7, 2014 at 15:59 Comment(6)
I'm not being able to make it work on IE 11, it just keeps showing me the Certificate Error pageJaxartes
For firefox 48+ using geckodriver still have issue ,this's open issue in geckodriver ,they still have no idea for it ,see the Bug IssueTimbal
This answer is no longer valid, use 'acceptInsecureCerts' insteadTarpon
This comment might be very late but helpful for people reaching the question now. I tried all of the above and nothing worked. Only managed to pass the Error with: driver.get("javascript:document.getElementById('overridelink').click()")Approach
for chromedriver I ended up passing all of these four strings to options.add_argument --> allow-running-insecure-content and ignore-certificate-errors and allow-insecure-localhost and unsafely-treat-insecure-origin-as-secure (you can try to find more by: strings /opt/google/chrome/chrome | grep insecure and similar grepping)Rozella
For me, firefox still showed the warning later on a page redirect/form submission, not at the beginning. Chrome worked fine.Lighterman
L
10

For Firefox:

ProfilesIni profile = new ProfilesIni();
FirefoxProfile myprofile = profile.getProfile("default");
myprofile.setAcceptUntrustedCertificates(true);
myprofile.setAssumeUntrustedCertificateIssuer(true);
WebDriver driver = new FirefoxDriver(myprofile);

For Chrome we can use:

DesiredCapabilities capabilities = DesiredCapabilities.chrome();
capabilities.setCapability("chrome.switches", Arrays.asList("--ignore-certificate-errors"));
driver = new ChromeDriver(capabilities);

For Internet Explorer we can use:

DesiredCapabilities capabilities = new DesiredCapabilities();
capabilities.setCapability(CapabilityType.ACCEPT_SSL_CERTS, true);      
Webdriver driver = new InternetExplorerDriver(capabilities);
Lunar answered 1/7, 2014 at 10:49 Comment(3)
The question was about Python. You could at least write what language is that.Hotchpotch
Be careful, 'ProfilesIni' is deprecated !Autoradiograph
Hope that java version may help ChromeOptions options = new ChromeOptions(); options .addArguments("--ignore-ssl-errors=yes", "--ignore-certificate-errors"); ChromeDriver driver = new ChromeDriver(options);Whitesmith
F
7

For Firefox Python:

The Firefox Self-signed certificate bug has now been fixed: accept ssl cert with marionette firefox webdrive python splinter

"acceptSslCerts" should be replaced by "acceptInsecureCerts"

from selenium import webdriver
from selenium.webdriver.common.desired_capabilities import DesiredCapabilities
from selenium.webdriver.firefox.firefox_binary import FirefoxBinary

caps = DesiredCapabilities.FIREFOX.copy()
caps['acceptInsecureCerts'] = True
ff_binary = FirefoxBinary("path to the Nightly binary")

driver = webdriver.Firefox(firefox_binary=ff_binary, capabilities=caps)
driver.get("https://expired.badssl.com")
Fountain answered 18/1, 2017 at 5:1 Comment(1)
And now Firefox 52 is live. Upgrade Firefox, upgrade selenium to v3.3, download geckodriver to v0.15 and you don't even need the binary path anymore!Factitious
E
6

For people coming to this question related to headless chrome via python selenium, you may find https://bugs.chromium.org/p/chromium/issues/detail?id=721739#c102 to be useful.

It looks like you can either do

chrome_options = Options()
chrome_options.add_argument('--allow-insecure-localhost')

or something along the lines of the following (may need to adapt for python):

ChromeOptions options = new ChromeOptions()
DesiredCapabilities caps = DesiredCapabilities.chrome()
caps.setCapability(ChromeOptions.CAPABILITY, options)
caps.setCapability("acceptInsecureCerts", true)
WebDriver driver = new ChromeDriver(caps)
Excavator answered 30/1, 2018 at 21:42 Comment(0)
R
6

And in C# (.net core) using Selenium.Webdriver and Selenium.Chrome.Webdriver like this:

ChromeOptions options = new ChromeOptions();
options.AddArgument("--ignore-certificate-errors");
using (var driver = new ChromeDriver(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location),options))
{ 
  ...
}
Retrace answered 5/1, 2019 at 20:17 Comment(0)
U
4
ChromeOptions options = new ChromeOptions().addArguments("--proxy-server=http://" + proxy);
options.setAcceptInsecureCerts(true);
Urfa answered 30/5, 2020 at 17:33 Comment(1)
While this code snippet may solve the question, including an explanation really helps to improve the quality of your post. Remember that you are answering the question for readers in the future, and those people might not know the reasons for your code suggestionOutspeak
N
2

Javascript:

const capabilities = webdriver.Capabilities.phantomjs();
capabilities.set(webdriver.Capability.ACCEPT_SSL_CERTS, true);
capabilities.set(webdriver.Capability.SECURE_SSL, false);
capabilities.set('phantomjs.cli.args', ['--web-security=no', '--ssl-protocol=any', '--ignore-ssl-errors=yes']);
const driver = new webdriver.Builder().withCapabilities(webdriver.Capabilities.chrome(), capabilities).build();
Nashner answered 27/7, 2016 at 19:27 Comment(0)
I
2

In selenium python, you need to set desired_capabilities as:

desired_capabilities = {
    "acceptInsecureCerts": True
}
Ichthyosaur answered 30/8, 2018 at 10:15 Comment(0)
I
2

I ran into the same issue with Selenium and Behat. If you want to pass the parameters via behat.yml, here is what it needs to look like:

default:
    extensions:
        Behat\MinkExtension:
            base_url: https://my-app.com
            default_session: selenium2
            selenium2:
                browser: firefox
                capabilities:
                    extra_capabilities:
                        acceptInsecureCerts: true
Inexperienced answered 18/6, 2019 at 6:44 Comment(0)
T
1

Creating a profile and then a driver helps us get around the certificate issue in Firefox:

var profile = new FirefoxProfile();
profile.SetPreference("network.automatic-ntlm-auth.trusted-uris","DESIREDURL");
driver = new FirefoxDriver(profile);
Teel answered 28/7, 2014 at 22:16 Comment(1)
what about Internet Explorer and Google Chrome ?Piave
B
1

For those who come to this issue using Firefox and the above solutions don't work, you may try the code below (my original answer is here).

from selenium import webdriver

profile = webdriver.FirefoxProfile()
profile.DEFAULT_PREFERENCES['frozen']['marionette.contentListener'] = True
profile.DEFAULT_PREFERENCES['frozen']['network.stricttransportsecurity.preloadlist'] = False
profile.DEFAULT_PREFERENCES['frozen']['security.cert_pinning.enforcement_level'] = 0
profile.set_preference('webdriver_assume_untrusted_issuer', False)
profile.set_preference("browser.download.folderList", 2)
profile.set_preference("browser.download.manager.showWhenStarting", False)
profile.set_preference("browser.download.dir", temp_folder)
profile.set_preference("browser.helperApps.neverAsk.saveToDisk",
                   "text/plain, image/png")
driver = webdriver.Firefox(firefox_profile=profile)
Birkle answered 4/12, 2018 at 15:42 Comment(0)
N
1
WebDriverManager.chromedriver().setup();
ChromeOptions options = new ChromeOptions();
options.addArguments("--ignore-certificate-errors");
driver = new ChromeDriver(options);

I have used it for Java with Chrome browser it is working nice

Nonaligned answered 21/3, 2020 at 14:17 Comment(1)
While this code may solve the question, including an explanation of how and why this solves the problem would really help to improve the quality of your post, and probably result in more up-votes. Remember that you are answering the question for readers in the future, not just the person asking now. Please edit your answer to add explanations and give an indication of what limitations and assumptions apply.Capitulate
A
0

Delete all but the necessary certificate from your browser's certificate store and then configure the browser to automatically select the certificate when only one certificate is present.

Appetency answered 20/5, 2015 at 0:29 Comment(0)
S
0

Just an update regarding this issue.

Require Drivers:

Linux: Centos 7 64bit, Window 7 64bit

Firefox: 52.0.3

Selenium Webdriver: 3.4.0 (Windows), 3.8.1 (Linux Centos)

GeckoDriver: v0.16.0 (Windows), v0.17.0 (Linux Centos)

Code

System.setProperty("webdriver.gecko.driver", "/home/seleniumproject/geckodrivers/linux/v0.17/geckodriver");

ProfilesIni ini = new ProfilesIni();


// Change the profile name to your own. The profile name can 
// be found under .mozilla folder ~/.mozilla/firefox/profile. 
// See you profile.ini for the default profile name

FirefoxProfile profile = ini.getProfile("default"); 

DesiredCapabilities cap = new DesiredCapabilities();
cap.setAcceptInsecureCerts(true);

FirefoxBinary firefoxBinary = new FirefoxBinary();

GeckoDriverService service =new GeckoDriverService.Builder(firefoxBinary)
    .usingDriverExecutable(new 
File("/home/seleniumproject/geckodrivers/linux/v0.17/geckodriver"))
    .usingAnyFreePort()
    .usingAnyFreePort()
    .build();
try {
    service.start();
} catch (IOException e) {
    e.printStackTrace();
}

FirefoxOptions options = new FirefoxOptions().setBinary(firefoxBinary).setProfile(profile).addCapabilities(cap);

driver = new FirefoxDriver(options);
driver.get("https://www.google.com");

System.out.println("Life Title -> " + driver.getTitle());
driver.close();
Sokul answered 9/5, 2018 at 5:35 Comment(0)
N
0

I was able to do this on .net c# with PhantomJSDriver with selenium web driver 3.1

 [TestMethod]
    public void headless()
    {


        var driverService = PhantomJSDriverService.CreateDefaultService(@"C:\Driver\phantomjs\");
        driverService.SuppressInitialDiagnosticInformation = true;
        driverService.AddArgument("--web-security=no");
        driverService.AddArgument("--ignore-ssl-errors=yes");
        driver = new PhantomJSDriver(driverService);

        driver.Navigate().GoToUrl("XXXXXX.aspx");

        Thread.Sleep(6000);
    }
Notochord answered 20/9, 2018 at 3:20 Comment(0)
G
0

Whenever I run into this issue with newer browsers, I just use AppRobotic Personal edition to click specific screen coordinates, or tab through the buttons and click.

Basically it's just using its macro functionality, but won't work on headless setups though.

Gorget answered 21/4, 2019 at 12:18 Comment(0)
I
0

I had the exact same issue. However when I tried opening the website manually in the browser the certificate was correct, but in the details the name was "DONOTTRUST".

The difference of certificate was caused by Fiddler that was running in background and decrypting all HTTPS content before reencrypting it.

To fix my problem, just close Fiddler on machine. If you need to keep Fiddler opened, then you can uncheck Decrypt SSL in Fiddler Settings.

Inferno answered 5/9, 2019 at 14:29 Comment(0)
N
0

For .NET, what worked for me was the following...

var chromeOptions = new ChromeOptions { AcceptInsecureCertificates = true };

Pretty much, it tells the ChromeDriver options not to halt browser execution when an insecure certificate is detected, and to proceed as normal.

Nondisjunction answered 8/6, 2021 at 0:30 Comment(0)
H
0

Simple approach,

from selenium import webdriver
from selenium.webdriver.chrome.service import Service as ChromeService

# pip install webdriver-manager
from webdriver_manager.chrome import ChromeDriverManager



def get_chrome_capabilities():
    caps = webdriver.DesiredCapabilities.CHROME
    caps['acceptSslCerts'] = True
    caps['acceptInsecureCerts'] = True
    opts = webdriver.ChromeOptions()
    caps.update(opts.to_capabilities())
    return caps


# ChromeDriveManager to automate and download webdriver
service = ChromeService(executable_path=ChromeDriverManager().install())
driver = webdriver.Chrome(
  service=service,
  desired_capabilities=get_chrome_capabilities(),
)

# Use this instead of the above if you are already setup
# driver = webdriver.Chrome(desired_capabilities=get_chrome_capabilities())


driver.get("http://www.google.com")
assert "google" in driver.page_source
driver.quit()
Hierophant answered 13/4, 2023 at 15:46 Comment(0)
L
-3

It looks like it still doesn't have a standard decision of this problem. In other words - you still can't say "Okay, do a certification, whatever if you are Internet Explorer, Mozilla or Google Chrome". But I found one post that shows how to work around the problem in Mozilla Firefox. If you are interested in it, you can check it here.

Lisabethlisan answered 1/7, 2014 at 10:31 Comment(1)
But what about the code above done in Java ? It is asking each browser to accept the certificate of the current visted website. Can not we do the same in Python ?Piave

© 2022 - 2024 — McMap. All rights reserved.