【问题标题】:Detect javascript console output with python用python检测javascript控制台输出
【发布时间】:2015-04-25 01:50:55
【问题描述】:

当我在一个大量 javascript 的应用程序中工作时,我喜欢使用 Console.log() 进行调试。但是,我想确保我已正确禁止生产中的所有控制台登录。

我想创建一个 python 脚本来遍历一个站点并生成一个向控制台写入内容的页面列表。我知道我可以让它在最小化之前翻阅 javascript 文件并搜索“Console.Log”,但我经常是项目的分析师,因此无法访问代码库。我们还有大量的网站有一段时间没有接触过,但可以使用扫描(很棒的实习项目 :-))。

这对 python 可行吗?

【问题讨论】:

  • 您可以使用urllib 读取源查找日志。
  • 一个足够坚定的对手可以永远阻止他们的控制台日志记录功能被检测到。前任。使用eval(rot13(pbafbyr.ybt("Uryyb"))),仅搜索“控制台”不会找到它。 100% 确保您的程序不记录的唯一方法是运行所有可能的代码路径以及所有可能的输入......但是当然,大多数程序不是由您的大敌编写的,所以只需寻找“ console.log" 应该能捕获 99% 的情况。
  • 执行console.log = console.warn = console.dir = console.debug = console.time = console.timeEnd = console.assert = function () {}; 之类的操作会抑制所有控制台日志记录
  • 谢谢@MalikBrahimi。我了解如何使用 urllib 在页面源中找到特定文本或元素,但我不明白如何找到控制台输出?如果 javascript 已被最小化,我不能只寻找“.log()”。

标签: javascript python


【解决方案1】:

您可以使用selenium browser automation tool 并检查控制台日志:

from selenium import webdriver
from selenium.webdriver.common.desired_capabilities import DesiredCapabilities    

capabilities = DesiredCapabilities.CHROME
capabilities['loggingPrefs'] = { 'browser':'ALL' }

driver = webdriver.Chrome(desired_capabilities=capabilities)

driver.get('http://foo.com')

# print console log messages
for entry in driver.get_log('browser'):
    print entry

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

【讨论】:

  • 谢谢@alecxe。这可能是一个很好的解决方案,尽管我宁愿不依赖于在机器上安装 chrome,我认为会是这种情况
  • @user1159067 不,您也可以使用FirefoxSafari、IE 或无头PhantomJS 浏览器。
  • 旁注:打印输出后要关闭窗口,请使用 driver.close()
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2021-04-10
  • 2018-05-26
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多