首页 > 解决方案 > 如何将 console.log 消息从 Firefox/Chrome webbrowser 控制台重定向到终端

问题描述

正如标题中所说,我想将出现在浏览器控制台中的 console.log 消息重定向到我的 windows 终端——例如 VSCode 中的内置终端。有没有办法通过一些内置的浏览器功能来实现这个结果,或者它宁愿需要我运行一些外部工具、代理后端或像 Selenium 这样的虚拟浏览器?问题涉及使用 webpack 制作的 React 应用程序。谢谢。

标签: reactjsgoogle-chromefirefoxwebpackbrowser

解决方案


记录在 javascript API 文档中

 driver.manage().logs().get(logging.Type.BROWSER)
     .then(function(entries) {
        entries.forEach(function(entry) {
          console.log('[%s] %s', entry.level.name, entry.message);
        });
     });

** 注意:** 只有少数浏览器支持远程日志记录 API(尤其是 Firefox 和 Chrome)。Firefox 支持基本的日志记录功能,而 Chrome 提供了强大的性能日志记录选项。远程日志记录仍然被认为是一个非标准特性,并且该模块为其公开的 API 是非冻结的。一旦 W3C WebDriver 规范正式定义日志记录,此模块将被更新,可能会破坏向后兼容性。

https://www.selenium.dev/selenium/docs/api/javascript/module/selenium-webdriver/lib/logging.html

你的问题被标记为 JavaScript,但你写的是关于 python,你可以在 Python 中以类似的方式记录日志,请参阅:Getting console.log output from Chrome with Selenium Python API bindingshttps://docs.python.org/3/如何/logging.html


推荐阅读