我有一个大型应用程序,我正在使用无头Chrome、Selenium和Python来测试每个模块。我想检查每个模块,并获取在该特定模块中生成的所有JS控制台错误。
但是,由于每个模块都在不同的测试用例中,并且每个用例在单独的会话中执行,所以脚本必须首先登录到每个测试中。登录进程本身会产生许多出现在控制台中的错误。在测试每个模块时,我不希望不相关的登录错误出现在日志中。
基本上,清除当前日志中的任何内容,->转到模块并执行已添加到控制台的-> get日志。
这不可能吗?我尝试执行driver.execute_script("console.clear()"),但是控制台中的消息没有被删除,并且在做了一些事情并打印日志之后,与登录相关的消息仍然显示出来。
发布于 2017-07-09 16:15:29
2017年和2018年末
日志API还不是官方Webdriver驱动程序规范的一部分。
实际上,它是用于第2级规范的要求定义。2017年年中,只有Chromedriver有一个无文档的、非标准的命令实现。
在源中,没有清除日志的方法的跟踪:
Webdriver.get_log()Command名称的RemoteConnection中转换为尖锐的请求可能的解决办法
返回的(原始)数据结构是如下所示的字典:
{
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发布于 2021-12-10 18:32:18
这个线程已经有几年的历史了,但是如果其他人发现自己正在试图解决类似的问题:
我还尝试使用driver.execute_script('console.clear()')清除我的登录过程和我想要检查的页面之间的控制台日志,但没有结果。
结果是,调用driver.get_log('browser')返回浏览器日志并清除它。
在浏览要忽略控制台日志的页面后,可以使用以下内容清除它们
_ = driver.get_log('browser')
https://stackoverflow.com/questions/44991009
复制相似问题