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?

4
  • You can use urllib to read the source looking for logs. Commented Feb 23, 2015 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. Commented Feb 23, 2015 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 Commented Feb 23, 2015 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()". Commented Feb 23, 2015 at 17:03

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

3 Comments

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
@user1159067 nope, you can also use Firefox, Safari, IE or a headless PhantomJS browser.
Side note: to close window after you have printed the output, use driver.close()

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.