使用Python的Selenium RemoteWebDriver进行性能日志记录?

6 投票
2 回答
5278 浏览
提问于 2025-04-18 18:33

我正在尝试从一个远程的webdriver实例获取一些性能日志信息。我使用的是Python的Selenium库。

根据我看到的,这些信息应该是可以获取的。我觉得这可能只有在使用ChromeDriver时才能实现。目前我在用Firefox,但如果能获取到我想要的信息,我可以很容易地切换到Chrome。

不过,我对Python还很陌生(但我在学习中!),关于在Python中使用的能力字典(用于性能日志记录)的文档似乎有点有限(或者今天我的搜索能力不太行)。

我找到了以下内容:

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代码。假设这是可能的。

有没有什么想法?

2 个回答

-1

我在研究Chromedriver的日志一段时间后,发现了一个更简洁的解决方案,这个方案不仅适用于Chrome浏览器,还能在其他浏览器上使用。

这个解决方案是:https://pypi.python.org/pypi/seleniumwrapper

它为导航时间API(https://dvcs.w3.org/hg/webperf/raw-file/tip/specs/NavigationTiming/Overview.html)提供了一个很好的封装。这样可以得到更紧凑的数据集,而且更容易理解和使用。

它对标准Selenium的其他封装也相当不错!

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' - 你在上面的代码片段中只看到了前者。后者只是返回你远程会话中可用的日志类型列表。)

现在我只需要理解它的意思……

撰写回答