How to access Security panel on google chrome developer tools with python selenium?

Question:

The following python code can get console log, but how can I access Security tab on Chrome devTools? E.g. I want to log about This page is secure or not (HTTPS) and the TLS certificate status.

enter image description here

from selenium import webdriver
from selenium.webdriver.common.desired_capabilities import DesiredCapabilities
import time

 yoururl = "test_url"

 caps = DesiredCapabilities.CHROME
 caps['goog:loggingPrefs'] = {'browser': 'ALL'}
 # driver = webdriver.Chrome(desired_capabilities=caps)
 driver = webdriver.Chrome('path_of_chromedriver', desired_capabilities=caps)

 driver.get(yoururl)
 time.sleep(5) # wait for all the data to arrive.
 for log in driver.get_log('browser'):
     print(log)
 driver.close()
Asked By: AHua Hsu

||

Answers:

Ok, my solution is as follows:

from selenium import webdriver
from selenium.webdriver.common.desired_capabilities import DesiredCapabilities
import time
import json

caps = DesiredCapabilities.CHROME.copy()
caps['goog:loggingPrefs'] = {
    'performance': 'ALL',
}
caps['goog:perfLoggingPrefs'] = {
    'enableNetwork': True
}
option = webdriver.ChromeOptions()
option.add_argument('--ignore-certificate-errors')
# option.add_experimental_option('w3c', False)

driver = webdriver.Chrome('chromedriver.exe', options=option, desired_capabilities=caps)
yoururl = "https://www.google.com.tw/"

driver.get(yoururl)
time.sleep(5)

for log_data in driver.get_log('performance'):
    log_json = json.loads(log_data['message'])
    log = log_json['message']
    if log['method'] == 'Network.responseReceived' and log['params']['response']['url'] == yoururl:
        print(f"securityState={log['params']['response']['securityState']}")
        print(f"securityDetails={log['params']['response']['securityDetails']}")
driver.quit()
Answered By: AHua Hsu
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.