21

I'm trying to follow the idea suggested in the Web Performance Testing with WebDriver google test automation conference talk and ChromeDriver "Performance Log" documentation page to get the trace data which I want to submit to webpagetest for performance analysis later.

How can I retrieve performance logs using python selenium bindings?


I've tried to print out log_types available in the driver instance

from selenium import webdriver

driver = webdriver.Chrome()
driver.get('https://stackoverflow.com')

print driver.log_types

driver.close()

but got only

[u'browser', u'driver']

And I don't see a relevant command-line switch.

alecxe
  • 462,703
  • 120
  • 1,088
  • 1,195

2 Answers2

38

Performance logs are disabled by default.

To enable it, use DesiredCapabilities and configure loggingPrefs:

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

caps = DesiredCapabilities.CHROME
 #as per latest docs
caps['goog:loggingPrefs'] = {'performance': 'ALL'}
driver = webdriver.Chrome(desired_capabilities=caps)

driver.get('https://stackoverflow.com')

for entry in driver.get_log('performance'):
    print(entry)

driver.quit()

This results into a bunch of tracing log entries printed on the console:

{u'timestamp': 1419487459178, u'message': u'{"message":{"method":"Network.responseReceived","params":{"frameId":"2105.1","loaderId":"2105.2","requestId":"2105.1","response":{"connectionId":0,"connectionReused":false,"encodedDataLength":-1,"fromDiskCache":false,"fromServiceWorker":false,"headers":{"Access-Control-Allow-Origin":"*","Content-Type":"text/plain;charset=US-ASCII"},"mimeType":"text/plain","status":200,"statusText":"OK","url":"data:,"},"timestamp":1419487458.92934,"type":"Document"}},"webview":"2C66E956-A48B-456B-8A4E-1022F699AA92"}', u'level': u'INFO'}
{u'timestamp': 1419487459178, u'message': u'{"message":{"method":"Network.loadingFinished","params":{"encodedDataLength":0,"requestId":"2105.1","timestamp":1419487458.92936}},"webview":"2C66E956-A48B-456B-8A4E-1022F699AA92"}', u'level': u'INFO'}
{u'timestamp': 1419487459178, u'message': u'{"message":{"method":"Page.frameNavigated","params":{"frame":{"id":"2105.1","loaderId":"2105.2","mimeType":"text/plain","securityOrigin":"://","url":"data:,"}}},"webview":"2C66E956-A48B-456B-8A4E-1022F699AA92"}', u'level': u'INFO'}
...
Rajesh
  • 48
  • 4
alecxe
  • 462,703
  • 120
  • 1,088
  • 1,195
  • 2
    I know this answer is super old, but do you know if there is a way to get only the "Network" events (i.e. what you'd see when you go to the "Network" tab on Chrome's Developer Tools) . I can parse what I need out of the log, but I'm thinking there is a more efficient way and I'm having some trouble figuring out the docs. Thanks! – Zach Feb 02 '19 at 17:01
  • Been looking for this answer for awhile now. Thanks! – West Jun 27 '19 at 03:36
  • 12
    For newer version of Chrome, you have to use `caps["goog:loggingPrefs"] = {"performance": "ALL"}`. – Garrett Hyde Jul 08 '19 at 05:12
  • The message entry inside the log is a string log need to be serialized to JSON. E.g. import json s = log['message'] json_acceptable_string = s.replace("'", "\"") d = json.loads(json_acceptable_string) – XRaycat Oct 11 '19 at 07:27
  • The API docs for `DesiredCapabilities` clearly state: "Note: Always use '.copy()' on the DesiredCapabilities object to avoid the side effects of altering the Global class instance." Therefore `caps = DesiredCapabilities.CHROME` should be `caps = DesiredCapabilities.CHROME.copy()` – Jugdish Aug 06 '22 at 21:32
0

Just to complement the @Rajesh answer, in case doesn't work you. It could be due you are using an old chromedriver version, so in that case, it would be loggingPrefs instead of goog:loggingPrefs.

caps['loggingPrefs'] = {'performance': 'ALL'}

For more info, look at this:

Getting console.log output from Chrome with Selenium Python API bindings

juanjosegdoj
  • 319
  • 3
  • 5