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
driver.get('http://foo.com')
# print messages for entry in
driver.get_log('browser'):
print(entry)





self.driver.get_log("performance")

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


Troubleshooting

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.

-> body = self.driver.execute_cdp_cmd("Network.getResponseBody", {"requestId": response['requestId']})
(Pdb) response['requestId']
'73287.104'
(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)

Comments