Libin Thomas
Libin Thomas

Reputation: 999

How to fetch details of website SSL certificate expiry, issuer etc using selenium python?

I want to fetch the certificate details of the webpage seen in chrome browser. Details such as:

enter image description here

I am unsure how to proceed further or where to look for the certificate. I tried to look for the same in network request but I suppose certificate details are not stored in network requests. I tried the below code:

from seleniumwire import webdriver
import pytest
from selenium.webdriver.chrome.options import Options
import time
import allure

class Test_main():

    @pytest.fixture()
    def test_setup(self):
        # initiating browser
        chrome_options = Options()
        chrome_options.binary_location=\
            r"C:\Users\libin.thomas\AppData\Local\Google\Chrome\Application\chrome.exe"
        chrome_options.add_argument('--start-maximized')
        chrome_options.add_argument('--headless')

        self.driver = webdriver.Chrome(executable_path=r"D:/Python/Sel_python/drivers/chromedriverv86/chromedriver.exe",options=chrome_options)
       

        # terminate script
        yield
        self.driver.close()
        self.driver.quit()
        print("Test completed")

    @allure.severity(allure.severity_level.BLOCKER)
    def testcase_01(self, test_setup):
        self.driver.get("https://lifesciences.cactusglobal.com/")
        title = self.driver.title
        print("Page title: "+title)

        #Capturing network requests
        for request in self.driver.requests:
            if request.response:
              print(
                  request.url,
                  request.response.status_code,
                  request.response.headers
              )

Is there anyway I can get details of SSL certificate using selenium or any Pypi package?

Upvotes: 1

Views: 1366

Answers (1)

Steffen Ullrich
Steffen Ullrich

Reputation: 123571

It is probably impossible to get these information using selenium since it is impossible to get these information from inside the browser using Javascript. One can try to access the website though directly using some Python code:

import ssl
 
conn = ssl.create_connection(('google.com',443))
ctx = ssl.create_default_context() 
conn = ctx.wrap_socket(conn, server_hostname = 'google.com')
print(conn.getpeercert())

The dictionary returned by getpeercert has all the necessary information about issuer and validity inside.

Note though that direct access by Python and by the browser (and thus by selenium) behave slightly differently because a different TLS stack and/or different settings regarding ciphers, supported TLS versions etc are used. In some cases this will lead to TLS handshake problems with the Python code which don't happen with the browser.

There are also cases where the certificates in the server are improperly setup and chain certificates are missing. Browser will often successfully work around this, Python not. In this case the code above will fail too and one might need to use more complex code, see Python getting common name from URL using ssl.getpeercert().

Upvotes: 2

Related Questions