19

I'm trying to get a web page's console.log output from Firefox via the python Selenium API bindings. Based on the code for Chrome, and some advice from the documentation, I tried the following:

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()

But, for even a simple example page that calls console.log("foo"), I don't see "foo" either in the log entries returned via the API or in the /tmp/firefox_console file. Am I doing something wrong? Or is this a Selenium limitation?

Community
  • 1
  • 1
msridhar
  • 2,846
  • 4
  • 22
  • 23
  • 3
    This is for people coming now to check answers. driver.get_log('browser') is not working for firefox as of this date. There is an open issue https://github.com/SeleniumHQ/selenium/issues/1161 – Rajesh Mar 17 '17 at 14:26
  • 2
    The issue mentioned above was closed in favor of a new one: https://github.com/mozilla/geckodriver/issues/284 - still unfixed. But Firefox 65+ introduced a "devtools.console.stdout.content" preference, which dumps the console.log output to stdout. – Nickolay Aug 22 '19 at 17:16

2 Answers2

6

Your code is correct when it comes to the get_log function, just add a print statement at the end like so:

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()

In fact:

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

returns 53 in my example with this as a sample entry in the list:

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

Seems like a bad char problem. As for why there is no output in the /tmp/firefox_console file, I have no clue, the logger seems to throw some webdriver debug info but no console.log output.

EDIT: Apparently the above code does not return data from console.log. It's not a Selenium bug as far as I can tell but a problem with Firefox. I managed to get around it by installing the Firebug along with ConsoleExport plugin for Firebug, then point it to some logging server. See also this SO answer for details on how to enable Firebug programmatically from Selenium.

See this gist for more details: https://gist.github.com/CGenie/fc63536a8467ae6ef945

Community
  • 1
  • 1
seeg
  • 1,608
  • 15
  • 9
  • 1
    I wasn't saying that I get no log output from the driver, just that I'm not getting the `console.log` output. Are you seeing `console.log` output in the driver log entries? – msridhar Aug 11 '14 at 18:26
  • 1
    It seems that uncatched exceptions and syntax errors are logged as SEVERE. This looks promising for catching JS errors with automated tests. – Andrea Ratto Aug 29 '14 at 11:58
1

There have been changes in implementation. I was also using:

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

but now I am using Python3.8, Selenium 3.4 and it is not working/implemented anymore. See another Stackoverflow question discussing it.

Otherwise you get the error: selenium.common.exceptions.WebDriverException: Message: loggingPrefs is not the name of a known capability or extension capability

dubaksk
  • 91
  • 2
  • 10