使用 Selenium Python API 绑定从 Chrome 获取 console.log 输出 [英] Getting console.log output from Chrome with Selenium Python API bindings

查看:107
本文介绍了使用 Selenium Python API 绑定从 Chrome 获取 console.log 输出的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 Selenium 通过 Python API 绑定在 Chrome 中运行测试,但我无法弄清楚如何配置 Chrome 以使加载的测试的 console.log 输出可用.我看到 WebDriver 对象上有 get_log()log_types() 方法,并且我看到了 获取 chrome 的控制台日志,其中显示了如何在 Java 中执行操作.但是我在 Python API 中没有看到与 Java 的 LoggingPreferences 类型等效的类型.有什么方法可以完成我所需要的吗?

I'm using Selenium to run tests in Chrome via the Python API bindings, and I'm having trouble figuring out how to configure Chrome to make the console.log output from the loaded test available. I see that there are get_log() and log_types() methods on the WebDriver object, and I've seen Get chrome's console log which shows how to do things in Java. But I don't see an equivalent of Java's LoggingPreferences type in the Python API. Is there some way to accomplish what I need?

推荐答案

好吧,终于想通了:

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 字段中.

Entries whose source field equals 'console-api' correspond to console messages, and the message itself is stored in the message field.

从 chromedriver, 75.0.3770.8 开始,你必须使用 goog:loggingPrefs 而不是 loggingPrefs:

Starting from chromedriver, 75.0.3770.8, you have to use goog:loggingPrefs instead of loggingPrefs:

d['goog:loggingPrefs'] = { 'browser':'ALL' }

这篇关于使用 Selenium Python API 绑定从 Chrome 获取 console.log 输出的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆