DemCodeLines
DemCodeLines

Reputation: 1920

Clear Chrome browser logs in Selenium/Python

I have a large application and I am using Headless Chrome, Selenium and Python to test each module. I want to go through each module and get all the JS console errors produced while inside that specific module.

However, since each module is inside a different test case and each case executes in a separate session, the script first has to login on every test. The login process itself produces a number of errors that show up in the console. When testing each module I don't want the unrelated login errors to appear in the log.

Basically, clear anything that is in the logs right now -> go to the module and do something -> get logs that have been added to the console.

Is this not possible? I tried doing driver.execute_script("console.clear()") but the messages in the console were not removed and the login-related messages were still showing after doing something and printing the logs.

Upvotes: 3

Views: 6371

Answers (2)

Dustin Eagar
Dustin Eagar

Reputation: 49

This thread is a few years old, but in case anyone else finds themselves here trying to solve a similar problem:

I also tried using driver.execute_script('console.clear()') to clear the console log between my login process and the page I wanted to check to no avail.

It turns out that calling driver.get_log('browser') returns the browser log and also clears it.

After navigating through pages for which you want to ignore the console logs, you can clear them with something like

_ = driver.get_log('browser')

Upvotes: 3

try-catch-finally
try-catch-finally

Reputation: 7634

State in 2017 and late 2018

The logging API is not part of the official Webdriver specification yet.

In fact, it's requested to be defined for the level 2 specification. In mid 2017 only the Chromedriver has an undocumented non-standard implementation of that command.

In the sources there's no trace of a method for clearing logs:


Possible Workaround

The returned (raw) data structure is a dictionary that looks like this:

{
    u'source': u'console-api',
    u'message': u'http://localhost:7071/console.html 8:9 "error"',
    u'timestamp': 1499611688822,
    u'level': u'SEVERE'
}

It contains a timestamp that can be remembered so that subsequent calls to get_log() may filter for newer timestamps.

Facade

class WebdriverLogFacade(object):

    last_timestamp = 0

    def __init__(self, webdriver):
        self._webdriver = webdriver

    def get_log(self):
        last_timestamp = self.last_timestamp
        entries = self._webdriver.get_log("browser")
        filtered = []

        for entry in entries:
            # check the logged timestamp against the 
            # stored timestamp
            if entry["timestamp"] > self.last_timestamp:
                filtered.append(entry)

                # save the last timestamp only if newer 
                # in this set of logs
                if entry["timestamp"] > last_timestamp:
                    last_timestamp = entry["timestamp"]

        # store the very last timestamp
        self.last_timestamp = last_timestamp

        return filtered

Usage

log_facade = WebdriverLogFacade(driver)
logs = log_facade.get_log()

# more logs will be generated

logs = log_facade.get_log()
# newest log returned only

Upvotes: 6

Related Questions