使用 Selenium Python API 绑定从 Chrome 获取 console.log 输出

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/20907180/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-18 21:31:26  来源:igfitidea点击:

Getting console.log output from Chrome with Selenium Python API bindings

pythongoogle-chromeloggingselenium

提问by msridhar

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.logoutput 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 logwhich shows how to do things in Java. But I don't see an equivalent of Java's LoggingPreferencestype in the Python API. Is there some way to accomplish what I need?

我正在使用 Selenium 通过 Python API 绑定在 Chrome 中运行测试,但我无法弄清楚如何配置 Chrome 以使console.log加载的测试的输出可用。我看到有get_log()log_types()的webdriver的对象的方法,我已经看到了获取Chrome浏览器的控制台日志怎么做这说明在Java中的东西。但是我LoggingPreferences在 Python API 中没有看到 Java类型的等价物。有什么方法可以完成我所需要的吗?

采纳答案by msridhar

Ok, finally figured it out:

好吧,终于想通了:

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)

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

source字段等于的条目'console-api'对应于控制台消息,消息本身存储在该message字段中。

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

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

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

回答by Perdu

To complete the answer: starting from chromedriver 75.0.3770.8, you have to use goog:loggingPrefsinstead of loggingPrefs.

要完成答案:从 chromedriver 75.0.3770.8 开始,您必须使用goog:loggingPrefs而不是loggingPrefs.

See Chromedriver changelog: http://chromedriver.chromium.org/downloadsor this bug: https://bugs.chromium.org/p/chromedriver/issues/detail?id=2976

请参阅 Chromedriver 更新日志:http://chromedriver.chromium.org/downloads或此错误:https://bugs.chromium.org/p/chromedriver/issues/detail?id=2976

回答by Jortega

Note that calling driver.get_log('browser')will cause the next call to return nothing until more logs are written to the console.

请注意,调用driver.get_log('browser')将导致下一次调用不返回任何内容,直到将更多日志写入控制台。

I would suggest saving the logs to a variable first. For example below logs_2will equal [].

我建议先将日志保存到变量中。例如下面logs_2将等于[].

If you need something in the console to test you can use:

如果您需要在控制台中进行测试,您可以使用:

self.driver.execute_script("""

                function myFunction() {
                      console.log("Window loaded")
                }

                if(window.attachEvent) {
            window.attachEvent('onload', myFunction());
        } else {
            if(window.onload) {
                var curronload = window.onload;
                var newonload = function(evt) {
                    curronload(evt);
                    myFunction(evt);
                };
                window.onload = newonload;
            } else {
                window.onload = myFunction();
            }
        }
                """)


logs_1 = driver.get_log('browser')
print("A::", logs_1 )
logs_2 = driver.get_log('browser')
print("B::", logs_2 )
for entry in logs_1:
    print("Aa::",entry)

for entry in logs_2:
    print("Bb::",entry)

See the answer from msridhar for what should go above my example code.

请参阅 msridhar 的答案,了解我的示例代码上方应包含的内容。