Webdriver read performance logs


If you need information from XHR requests from testing page you can use logs from chrome driver.

from selenium import webdriver
from selenium.webdriver.common.desired_capabilities import DesiredCapabilities
# enable browser
logging d = DesiredCapabilities.CHROME
d['loggingPrefs'] = { 'browser':'ALL' }
driver = webdriver.Chrome(desired_capabilities=d)
# load the desired webpage
# print messages for entry in


{"requestId":max(xhrs, key=lambda x:x['id']).get("id")}) 


Get details from an working example 

def get_details(self, job):

       logs_json = []

       responses = []





       logs = self.driver.get_log("performance")

       body = ""

       for log in logs:



       for log in logs_json:

           if(log['method'] == 'Network.responseReceived'):



       api_url = job.url.replace("https://www.domain.be/", "https://www.domain.be/api/")

       api_url = api_url + "?refresh=false"


       for response in responses:

           if(response['response']['url'].find(api_url) != -1): # if right request load body then

                   # Use second request first is expaired with cookies.




                       body = self.driver.execute_cdp_cmd("Network.getResponseBody", {"requestId": response['requestId']})

                       json_body = json.loads(body['body'])





Sometimes you can sit for quite a while with an error where you cannot read the body of a request. In my experience that was a problem of multiple requests. If on a page multiple requests are made to the same URL then it could be that previous response is no longer readable and body cannot be found. Then this error happens. So you have to get your body not only from right request but on time.

-> body = self.driver.execute_cdp_cmd("Network.getResponseBody", {"requestId": response['requestId']})
(Pdb) response['requestId']
(Pdb) body = self.driver.execute_cdp_cmd("Network.getResponseBody", {"requestId": response['requestId']})
* selenium.common.exceptions.WebDriverException: Message: unknown error: unhandled inspector error: {"code":-32000,"message":"No resource with given identifier found"}
  (Session info: chrome=100.0.4896.127)