4

When I'm working in a javascript-heavy application I like to use Console.log() a lot for debugging. However, I want to ensure I've properly suppressed all console logging in production.

I'd like to create a python script to walk through a site and generate a list of pages that write something to the console. I know I could just have it rip through the javascript files before minimization and search for "Console.Log" but I'm often the analyst on a project and consequently don't have access to the codebase. We also have a ton of sites that haven't been touched in a while but could use a scan (great intern project :-) ).

Is this doable with python?

Elliott
  • 2,035
  • 20
  • 23
  • You can use `urllib` to read the source looking for logs. – Malik Brahimi Feb 23 '15 at 16:52
  • A sufficiently determined opponent could forever keep their console logging functionality from being detected. Ex. with `eval(rot13(pbafbyr.ybt("Uryyb")))`, just doing a search for "console" won't find it. The only way to be 100% sure that your program doesn't log, is to run all possible code paths with all possible inputs... But of course, most programs are not written by your arch-enemy, so just looking for "console.log" ought to catch 99% of cases. – Kevin Feb 23 '15 at 16:56
  • doing something like `console.log = console.warn = console.dir = console.debug = console.time = console.timeEnd = console.assert = function () {};` etc would supress all console logging – Paul S. Feb 23 '15 at 16:58
  • Thanks @MalikBrahimi. I understand how I could find particular text or element in the page source with urllib but I don't understand how I would find console output? If the javascript has been minimized I can't just look for ".log()". – Elliott Feb 23 '15 at 17:03

1 Answers1

8

What you can do is to use selenium browser automation tool and check the console logs:

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

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

Community
  • 1
  • 1
alecxe
  • 462,703
  • 120
  • 1,088
  • 1,195
  • Thanks @alecxe. This could be a good solution though I'd rather not be dependent on having chrome installed on the machine, which I assume would be the case – Elliott Feb 23 '15 at 17:07
  • @user1159067 nope, you can also use `Firefox`, `Safari`, IE or a headless `PhantomJS` browser. – alecxe Feb 23 '15 at 17:08
  • Side note: to close window after you have printed the output, use driver.close() – mbunch Dec 15 '17 at 17:19