Skip to content

Instantly share code, notes, and snippets.

@matt2005
Forked from bcarroll/ChromeWebDriver.py
Last active June 10, 2022 12:28
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save matt2005/573e03e8c696f894ade9a293194874c9 to your computer and use it in GitHub Desktop.
Save matt2005/573e03e8c696f894ade9a293194874c9 to your computer and use it in GitHub Desktop.
Selenium Python - Get HTTP Status Code
"""
Implementation of the Selenium Chrome WebDriver
with HTTP Response data included via the ChromeDriver performance logging capability
https://sites.google.com/a/chromium.org/chromedriver/logging/performance-log
The ChromeWebDriver response attribute(s) contain a dict with information about the response
{
"connectionId": [Integer],
"connectionReused": [Boolean],
"encodedDataLength": [Integer],
"fromDiskCache": [Boolean],
"fromServiceWorker": [Boolean],
"headers": [dict], # HTTP Headers as a dict
"headersText": [String], # HTTP Headers as text
"mimeType": [String],
"protocol": [String],
"remoteIPAddress": [String],
"remotePort": [Integer],
"requestHeaders": [dict],
"requestHeadersText": [String],
"securityDetails": [dict], # TLS/SSL related information
"securityState": [String],
"status": [Integer], # HTTP Status Code of the Response
"statusText": [String],
"timing": [dict],
"url": [String]
}
Example:
from ChromeWebDriver import ChromeWebDriver
browser = ChromeWebDriver('https://github.com', headless=False)
print(browser.response["status"]) # prints the HTTP status code of the response (for the url in the class initialization)
for response in browser.responses: # the responses attribute contains a list of dicts containing all responses received (css, js, etc.)
print(response) # prints a dict containing data for each response
"""
import json
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.chrome.service import Service
from selenium.webdriver.common.desired_capabilities import DesiredCapabilities
from webdriver_manager.chrome import ChromeDriverManager
class ChromeWebDriver():
def __init__(self, url, headless=False, autoclose=True):
self.url = url # URL to fetch
self.options = webdriver.ChromeOptions()
self.service = Service(ChromeDriverManager().install())
self.options.headless = headless
self.desired_capabilities = DesiredCapabilities.CHROME
self.desired_capabilities['goog:loggingPrefs'] = {'performance': 'ALL'}
self.driver = webdriver.Chrome(service=self.service,options=self.options, desired_capabilities=self.desired_capabilities)
self.driver.get(url) # get the requested URL
self.responses = [] # list to store each response
self.response = ""
perfLog = self.driver.get_log('performance')
for logIndex in range(0, len(perfLog)): # Parse the Chrome Performance logs
logMessage = json.loads(perfLog[logIndex]["message"])["message"]
if logMessage["method"] == "Network.responseReceived": # Filter out HTTP responses
if logIndex == 0:
self.response = logMessage["params"]["response"]
self.responses.append(logMessage["params"]["response"]) # append each response to self.responses
if logMessage["params"]["response"]["url"] == self.url: # create instance attributes containing the response call for self.url
self.response = logMessage["params"]["response"]
self.driver.close()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment