Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Getting console.log output from Firefox with Selenium

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?

like image 383
msridhar Avatar asked Apr 22 '14 23:04

msridhar


People also ask

Does selenium work with Firefox?

Selenium IDE by SeleniumIt is implemented as a Firefox extension, and allows you to record, edit, and debug tests.

What is GeckoDriver log?

geckodriver provides different bands of logs for different audiences. The most important log entries are shown to everyone by default, and these include which port geckodriver provides the WebDriver API on, as well as informative warnings, errors, and fatal exceptions.


1 Answers

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

like image 189
seeg Avatar answered Sep 18 '22 05:09

seeg