Python中使用Selenium RemoteWebDriver - 性能日志记录?

6
我正在尝试从远程 webdriver 实例中获取一些性能日志信息。我正在使用 Python 的 Selenium 绑定库。
据我所见,这是我应该能够得到的信息。我认为它可能只适用于 ChromeDriver。我目前正在使用 FireFox 但可以轻松切换到 ChromeDriver 如果它能提供我想要的信息。
然而,我对 Python 还很陌生(但正在学习!)并且关于 Python 的 capabilities dictionaries(在性能日志记录时使用)的文档似乎有点有限(或者我的谷歌搜索技巧今天早上比较弱)。
我找到了以下内容:
DesiredCapabilities caps = DesiredCapabilities.chrome();
LoggingPreferences logPrefs = new LoggingPreferences();
logPrefs.enable("performance", Level.INFO);
caps.setCapability(CapabilityType.LOGGING_PREFS, logPrefs);
driver = new RemoteWebDriver("http://localhost:9515", caps);

这似乎可以满足我所需的。但是这是Java语言的代码。我不太确定如何将其转换为Python代码。假设可以转换,请问您有什么想法吗?


D'oh。发布5分钟后,我找到了这个:https://gist.github.com/klepikov/5457750.... 它提供了一些Python示例代码。所以我要去玩一下那个…… - ColinMcC
2个回答

8

如果有人想知道,我使用以下方法解决了这个问题:

(假设您正在使用远程selenium)

url = 'http://remote instance IP:PORT/wd/hub'
descaps = {'browserName': 'chrome', 'loggingPrefs': {'performance': 'INFO'}}

driver = webdriver.Remote(command_executor=url, desired_capabilities=descaps)

driver.command_executor._commands.update({'getAvailableLogTypes': 
                        ('GET', '/session/sessionId/log/types'), 
                        {'getLog': ('POST', '/session/$sessionId/log')})

getlog = driver.execute('getLog', {'type': 'performance'})['value']

(在添加了“getAvailableLogTypes”和“getLog”这两个命令中——您只会在上面的代码片段中看到前者。后者只是返回远程会话上可用日志类型的列表。)
现在,我所需要做的就是解释它……

你可以使用以下代码定义浏览器的能力:caps = webdriver.DesiredCapabilities.CHROME.copy(); caps['loggingPrefs'] = {'performance': 'INFO'}; driver = webdriver.Remote(command_executor=url, desired_capabilities=capabilities) - Jorge Orpinel Pérez
@JorgeOrpinel 我认为你的评论有一个错别字:desired_capabilities=caps) - Darren Parker

-1

请详细说明。我实际上并没有看到这在时序 API 方面添加任何有用的东西。 - Henrik Heimbuerger

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接