我正在使用Selenium通过Python API绑定(bind)在Chrome中运行测试,并且在弄清楚如何配置Chrome以使加载的测试中的console.log输出可用时遇到了麻烦。我看到WebDriver对象上有get_log()log_types()方法,并且我看到了Get chrome's console log,它显示了如何在Java中执行操作。但是我在Python API中看不到Java的LoggingPreferences类型的等效项。有什么办法可以满足我的需求?

最佳答案

好了,终于明白了:

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)
source字段等于'console-api'的条目对应于控制台消息,并且消息本身存储在message字段中。

从chromedriver 75.0.3770.8开始,您必须使用goog:loggingPrefs而不是loggingPrefs:
d['goog:loggingPrefs'] = { 'browser':'ALL' }

07-24 18:19
查看更多