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?
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?
Share Improve this question asked Feb 23, 2015 at 16:49 ElliottElliott 2,2251 gold badge21 silver badges24 bronze badges 4-
You can use
urllib
to read the source looking for logs. – Malik Brahimi 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. – Kevin 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 – Paul S. 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()". – Elliott Commented Feb 23, 2015 at 17:03
1 Answer
Reset to default 8What you can do is to use selenium
browser automation tool and check the console logs:
from selenium import webdriver
from selenium.webdriver.mon.desired_capabilities import DesiredCapabilities
capabilities = DesiredCapabilities.CHROME
capabilities['loggingPrefs'] = { 'browser':'ALL' }
driver = webdriver.Chrome(desired_capabilities=capabilities)
driver.get('http://foo.')
# 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.