There is a common scenario we all must have encountered i.e
sometimes when we try to load a web page it doesn't get loaded in fact
shows a message that "your communication is not secure". and if we load
it again sometimes it gets loaded up. this happens because of
Certificate error.
In this article, we are
going to learn about certificate error why it happens and how to handle
websites certification while working with selenium
What is Certificate ?
whenever
you visit any website who is HTTPS secured(not in case of HTTP) your
browser communicates with the site server to ensure that site is
encrypted to help in ensuring data privacy, but before starting to
ensure encrypted communication, the website will present browser with a
certificate to identify itself.
The browser will validate the
website%u2019s certificate by checking that the
certificate that signed it is valid, and checking that the certificate
that signed the parent certificate is valid and so forth up to a root
the certificate that is known to be valid is sometimes also called
Certificate Hierarchy and if the browser fails to do so connection gets
interrupted and an error message is displayed.
Why this error occur ?
If
the certificate that is presented by the server somehow is not able to
validate or if the encryption itself is not strong enough, your browser
will automatically stop the connection with the website and shows you an
error page with the message "your connection is not secure"
Following are the primary reason due to which Certificate error occurs:
- Certificate does not come from a trusted source
- The certificate will not be valid until the date
- The certificate expired on the date
- The issuer certificate is unknown and hence it is not trusted
- The certificate is not trusted because it is self-signed
- The given certificate is only valid for the particular site name
- Corrupted certificate store
Therefore, whenever we make our script and test in our own environment we will always get this error as certificates are costly and may not be used for test environments. Most
of the times, to test web applications over https, self-signed
certificates are used. And due to the fact that %u2018self-signed
certificates are not trusted%u2019 by browsers, we get the error in our test
environment while executing test scripts.
Handle this error in Selenium:
There
are many ways to solve this problem, I am using firefox browser you can
perform this on any browser just make sure to create the instance of
class webdriver of that browser
Method 1:
You can set "accept_untruster_certs" value of firefox to be True this
will allow accepting untrusted certificates also. below is a given
example that shows how to do this.
from selenium import webdriver
profile = webdriver.FirefoxProfile()
profile.accept_untrusted_certs = True
driver = webdriver.Firefox(firefox_profile=profile)
driver.get('https://url')
print driver.title
driver.close()
Method 2: we can use DesiredCapabilities to set 'acceptInsecureCerts' capability to true and this method works for all browsers
from selenium import webdriver
from selenium.webdriver.common.desired_capabilities import DesiredCapabilities
desired_capabilities = DesiredCapabilities.FIREFOX.copy()
desired_capabilities['acceptInsecureCerts'] = True
driver = webdriver.Firefox(capabilities=desired_capabilities)
driver.get('https://url')
print driver.title
driver.close()
Comments