【问题标题】:Getting console.log output from Firefox with Selenium使用 Selenium 从 Firefox 获取 console.log 输出
【发布时间】:2014-06-07 13:20:56
【问题描述】:

我正在尝试通过 python Selenium API 绑定从 Firefox 获取网页的 console.log 输出。基于code for Chrome和一些advice from the documentation,我尝试了以下:

from selenium import webdriver
from selenium.webdriver.common.desired_capabilities import DesiredCapabilities   
d = DesiredCapabilities.FIREFOX
d['loggingPrefs'] = { 'browser':'ALL' }
fp = webdriver.FirefoxProfile()
fp.set_preference('webdriver.log.file', '/tmp/firefox_console')
driver = webdriver.Firefox(capabilities=d,firefox_profile=fp)
driver.set_window_size(1280,1024)
driver.get('http://foo.com')
try:
    WebDriverWait(driver,10).until(lambda driver: driver.execute_script("return document.readyState") == "complete")
    for entry in driver.get_log('browser'):
        print entry
finally:
    driver.quit()

但是,即使是调用console.log("foo") 的简单示例页面,我也没有在通过API 返回的日志条目或/tmp/firefox_console 文件中看到"foo"。难道我做错了什么?或者这是硒的限制?

【问题讨论】:

  • 这是为现在来检查答案的人准备的。 driver.get_log('browser') 截至目前,它不适用于 firefox。有一个未解决的问题github.com/SeleniumHQ/selenium/issues/1161
  • 上述问题已被关闭,有利于新问题:github.com/mozilla/geckodriver/issues/284 - 仍未修复。但是 Firefox 65+ 引入了“devtools.console.stdout.content”首选项,它将console.log 输出转储到标准输出。

标签: python firefox logging selenium


【解决方案1】:

实施方面发生了变化。我也在使用:

d = DesiredCapabilities.FIREFOX
d['loggingPrefs'] = {'browser': 'ALL'}

但现在我使用的是 Python3.8、Selenium 3.4,它不再工作/实现了。 请参阅另一个 Stackoverflow question 讨论它。

否则会报错: selenium.common.exceptions.WebDriverException: Message: loggingPrefs is not the name of a known capability or extension capability

【讨论】:

    【解决方案2】:

    当涉及到get_log 函数时,您的代码是正确的,只需在末尾添加一个print 语句,如下所示:

    from selenium import webdriver
    from selenium.webdriver.common.desired_capabilities import DesiredCapabilities
    
    # enable browser logging
    d = DesiredCapabilities.FIREFOX
    d['loggingPrefs'] = {'browser': 'ALL'}
    driver = webdriver.Firefox(capabilities=d)
    # load some site
    driver.get('http://foo.com')
    # print messages
    for entry in driver.get_log('browser'):
        print entry
    
    print
    
    driver.quit()
    

    事实上:

    print len(driver.get_log('browser'))
    

    在我的示例中返回 53,并将其作为列表中的示例条目:

    {u'timestamp': 1407591650751, u'message': u"Expected ':' but found '}'.  Declaration dropped.", u'level': u'WARNING'}
    

    似乎是一个糟糕的字符问题。至于为什么/tmp/firefox_console文件没有输出,我不知道,logger似乎抛出了一些webdriver调试信息但没有console.log输出。

    编辑:显然上面的代码没有从console.log返回数据。据我所知,这不是 Selenium 错误,而是 Firefox 的问题。我设法通过为 Firebug 安装 FirebugConsoleExport 插件来解决它,然后将其指向某个日志服务器。有关如何从 Selenium 以编程方式启用 Firebug 的详细信息,另请参阅 this SO answer

    有关详细信息,请参阅此要点:https://gist.github.com/CGenie/fc63536a8467ae6ef945

    【讨论】:

    • 我并不是说我没有得到驱动程序的日志输出,只是我没有得到console.log 输出。您是否在驱动程序日志条目中看到 console.log 输出?
    • 似乎未捕获的异常和语法错误被记录为严重。这看起来很有希望通过自动化测试来捕获 JS 错误。
    猜你喜欢
    • 2014-01-21
    • 1970-01-01
    • 2023-03-12
    • 1970-01-01
    • 1970-01-01
    • 2021-05-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多