一尘不染

获取Chrome性能和跟踪日志

selenium

我正在尝试遵循Web性能测试与WebDriver谷歌测试自动化大会演讲和ChromeDriver
“性能日志”文档页面中提出的想法,以获取要提交给webtest稍后进行性能分析的跟踪数据。

如何使用 pythonselenium绑定 检索性能日志?


我试图打印出log_types驱动程序实例中可用的

from selenium import webdriver

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

print driver.log_types

driver.close()

但只有

[u'browser', u'driver']

而且我没有看到相关的命令行开关


阅读 699

收藏
2020-06-26

共1个答案

一尘不染

默认情况下, 性能日志是 禁用的

要启用它,请使用DesiredCapabilities并配置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()

这导致在控制台上打印了一堆跟踪日志条目:

{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'}
...
2020-06-26