2

I am making a selenium test suite (automated UI testing framework) using python. I want to get the browser console logs for my tests run on the IE and Edge webdrivers. To do this in chrome I do something like:

self.browser = webdriver.Chrome
d = DesiredCapabilities.CHROME  # Start with default Chrome capabilites
d['loggingPrefs'] = {'browser': 'ALL'}  # Add flag to track logs
# And then later on I dump logs with the return value from this selenium method:
self.browser.get_log('browser')

I have read a ton of old posts saying that IE doesn't support Getting console logs. I say old because the newest post I could find was from 2015. I was wondering if that has changed? Does anyone working with a modern selenium test suite know how to get console logs from the IE webdriver? Or Edge? Using a similar self.browser.get_log('browser') on Edge gives me an error "Not implemented"

Is there no point in trying to get console logs from IE/Edge? Or is there a workaround I am not aware of?

After discussion below I have tried a solution involving overriding the console.log method but I cant seem to figure out how to return the value to the webdriver. This is what I did:

    driver.execute_script('x = {}')  # Make an empty JS dict
    # Override the console.log method. This just appends the msg to x as an incrementing key
    driver.execute_script("console.log = function(msg){var key=Object.keys(x).length + 1; x[key] = msg;}")
    driver.execute_script('console.log("whatever")')  # Log something
    print(driver.execute_script('x'))  # I thought this would return the value of "x" but it returns None
Rosey
  • 739
  • 1
  • 12
  • 27
  • I don't know what exactly on console you want to catch, but there's a common solution to overwrite `console.log` with your custom code, using JavascriptExecutor, so every statement written to `console.log` actually goes where you want it to (for example inject `
    ` on page, and send all logs there; when you ar about to move from page, get div's content. Works on all browsers.
    – timbre timbre Nov 01 '18 at 16:26
  • What do you mean by JSExecutor? driver.execute_script()? Is there a command that gets everything in the log? – Rosey Nov 01 '18 at 16:45
  • 2
    yes, `driver.execute_script()` in python -land. Regarding your second question ("Is there a command that gets everything in the log?"): why do you see everything on console in the browser? because java script code sent it to be written there. How did it send it to be written there? using `console.log`. So by overwriting `console.log` to your arbitrary destination, you can take control of where the stuff is written. In other words, you are switching from standard to custom console. See https://stackoverflow.com/questions/4539253/what-is-console-log – timbre timbre Nov 01 '18 at 17:13
  • 1
    I tried what you suggested, and I think it works, I just don't know how to get the value of my JS variable to the webdriver. – Rosey Nov 01 '18 at 18:25
  • you can either `return ...` from the script, or write logs into injected hidden div (`document.getElementById("parentElement").innerHTML = '
    ';`), and get them via driver.getElementById("mylog")
    – timbre timbre Nov 01 '18 at 20:53

0 Answers0