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

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


Ok, finally figured it out:

from selenium import webdriverfrom selenium.webdriver.common.desired_capabilities import DesiredCapabilities# enable browser loggingd = DesiredCapabilities.CHROMEd['loggingPrefs'] = { 'browser':'ALL' }driver = webdriver.Chrome(desired_capabilities=d)# load the desired webpagedriver.get('http://foo.com')# print messagesfor entry in driver.get_log('browser'):    print(entry)

Entries whose source field equals 'console-api' correspond to console messages, and the message itself is stored in the message field.

Starting from chromedriver, 75.0.3770.8, you have to use goog:loggingPrefs instead of loggingPrefs:

d['goog:loggingPrefs'] = { 'browser':'ALL' }


To complete the answer: starting from chromedriver 75.0.3770.8, you have to use goog:loggingPrefs instead of loggingPrefs.

See Chromedriver changelog: http://chromedriver.chromium.org/downloads or this bug: https://bugs.chromium.org/p/chromedriver/issues/detail?id=2976


if you are using the python logging module (and you should be)... here is a way to add the selenium browser logs to the python logging system..

the get_browser_log_entries() function grabs the logs from eth provded driver, emits them to the python logging module as chrome. (ie chrome.console-api, chrome.network etc..) using the timestamp from the browser.(in case there is a delay before you call get_log)

it could probably do with some better exception handling (like if logging is not turned on ) etc.. but it works most of the time..

hop

import loggingfrom selenium import webdriverdef get_browser_log_entries(driver):    """get log entreies from selenium and add to python logger before returning"""    loglevels = { 'NOTSET':0 , 'DEBUG':10 ,'INFO': 20 , 'WARNING':30, 'ERROR':40, 'SEVERE':40, 'CRITICAL':50}    #initialise a logger    browserlog = logging.getLogger("chrome")    #get browser logs    slurped_logs = driver.get_log('browser')    for entry in slurped_logs:        #convert broswer log to python log format        rec = browserlog.makeRecord("%s.%s"%(browserlog.name,entry['source']),loglevels.get(entry['level']),'.',0,entry['message'],None,None)        rec.created = entry['timestamp'] /1000 # log using original timestamp.. us -> ms        try:            #add browser log to python log            browserlog.handle(rec)        except:            print(entry)    #and return logs incase you want them    return slurped_logsdef demo():    caps = webdriver.DesiredCapabilities.CHROME.copy()    caps['goog:loggingPrefs'] = { 'browser':'ALL' }    driver = webdriver.Chrome(desired_capabilities=caps )    driver.get("http://localhost")    consolemsgs = get_browser_log_entries(driver)if __name__ == "__main__":    logging.basicConfig(level=logging.DEBUG, format='%(asctime)s:%(levelname)7s:%(message)s')    logging.info("start")    demo()    logging.info("end")