繁体   English   中英

在 Selenium/Python 中清除 Chrome 浏览器日志

[英]Clear Chrome browser logs in Selenium/Python

我有一个大型应用程序,我使用 Headless Chrome、Selenium 和 Python 来测试每个模块。 我想查看每个模块并获取在该特定模块内产生的所有 JS 控制台错误。

但是,由于每个模块都在不同的测试用例中,并且每个用例都在单独的会话中执行,因此脚本必须首先登录每个测试。 登录过程本身会产生许多显示在控制台中的错误。 在测试每个模块时,我不希望无关的登录错误出现在日志中。

基本上,立即清除日志中的所有内容 -> 转到模块并执行某些操作 -> 获取已添加到控制台的日志。

这不可能吗? 我尝试执行driver.execute_script("console.clear()")但控制台中的消息没有被删除,并且在执行某些操作并打印日志后仍然显示与登录相关的消息。

2017年和2018年末的状态

日志记录API尚未成为官方Webdriver规范的一部分。

实际上, 要求为 2级规范进行定义 在2017年中,仅Chromedriver对该命令进行了未记录的非标准实现。

在源代码中没有清除日志的方法的痕迹:


可能的解决方法

返回的(原始)数据结构是一个字典,如下所示:

{
    u'source': u'console-api',
    u'message': u'http://localhost:7071/console.html 8:9 "error"',
    u'timestamp': 1499611688822,
    u'level': u'SEVERE'
}

它包含一个可以记住的时间戳,以便随后对get_log()调用可以过滤出较新的时间戳。

正面

class WebdriverLogFacade(object):

    last_timestamp = 0

    def __init__(self, webdriver):
        self._webdriver = webdriver

    def get_log(self):
        last_timestamp = self.last_timestamp
        entries = self._webdriver.get_log("browser")
        filtered = []

        for entry in entries:
            # check the logged timestamp against the 
            # stored timestamp
            if entry["timestamp"] > self.last_timestamp:
                filtered.append(entry)

                # save the last timestamp only if newer 
                # in this set of logs
                if entry["timestamp"] > last_timestamp:
                    last_timestamp = entry["timestamp"]

        # store the very last timestamp
        self.last_timestamp = last_timestamp

        return filtered

用法

log_facade = WebdriverLogFacade(driver)
logs = log_facade.get_log()

# more logs will be generated

logs = log_facade.get_log()
# newest log returned only

该线程已有几年历史,但以防万一其他人发现自己在这里试图解决类似的问题:

我还尝试使用driver.execute_script('console.clear()')清除我的登录过程和我想检查的页面之间的控制台日志无济于事。

事实证明,调用driver.get_log('browser')返回浏览器日志并清除它

在浏览要忽略控制台日志的页面后,您可以使用类似的方法清除它们

_ = driver.get_log('browser')

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM