17

Simple question: how to completely disable logging when using Selenium from Python bindings, ex code as follows:

browser = webdriver.Chrome()

I've tried things like:

options = webdriver.ChromeOptions();
options.add_argument('--log-level 3') 
browser = webdriver.Chrome(chrome_options=options)

or even:

options = webdriver.ChromeOptions();
options.add_argument('--disable-logging') 
browser = webdriver.Chrome(chrome_options=options)

but still the file 'chromedriver.log' is appearing on each new run of the tests.

9 Answers 9

28

You may set options.add_argument("--log-level=3") for Chrome browser to be run with Selenuim, or you may set logging level to some higher level with:

import logging
logger = logging.getLogger('selenium.webdriver.remote.remote_connection')
logger.setLevel(logging.WARNING)  # or any variant from ERROR, CRITICAL or NOTSET

But some messages will appear anyway in this case, including the starting DevTools message or SSL handshake error messages.

To run Chrome browser with Selenium in console in completely silent mode, you should use this snippet:

options = Options()
options.headless = True
options.add_experimental_option("excludeSwitches", ["enable-logging"])

That trick will suppress any console message from either the Selenium driver or the browser itself, including the first message DevTools listening on ws://127.0.0.1 at the very start.

At the same time some runtime step-by-step data can be saved to service log file, in case its argument has been added.

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

1 Comment

What about Edgdriver?
13
driver = webdriver.Chrome(service_log_path='/dev/null')

Comments

8

The source code of Chrome's webdriver, shows the existence of an option called service_log_path.

So if you want to get rid of the file, you could set this property to

  • /dev/null if you are running under Linux/Unix ;
  • NUL under windows

Hope it helps

Comments

7

Just example for Windows people:

webdriver.Firefox(log_path='NUL')

Accepted answer is correct, but if you are new to Python / windows like i am, example like this will cut you few hours of google time.

3 Comments

I have not tested this, but I'm pretty sure you could use logpath=os.devnull and it will work independent of OS. (Need to import os, of course).
I have tested this, and it works. I was getting multi-gigabyte logs and it was killing our servers. Now, nothing at all. Thanks @codingatty
Not working in my machine (Manjaro latest). Tried both os.devnull and NUL. My code was this driver = webdriver.Firefox(log_path=os.devnull, options=options, capabilities=capabilities, firefox_profile=profile)
6

To disable logging using Selenium and Python you need to add an experimental option through an instance of ChromeOptions() as follows:

add_experimental_option('excludeSwitches', ['enable-logging'])

Implementation

compatible code

from selenium import webdriver
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.chrome.service import Service

options = Options()
options.add_experimental_option('excludeSwitches', ['enable-logging'])
s = Service('C:\\BrowserDrivers\\chromedriver.exe')
driver = webdriver.Chrome(service=s, options=options)

Comments

3

this worked for me:

chrome_options.add_experimental_option('excludeSwitches', ['enable-logging'])

courtesy of:

https://joshuatz.com/posts/2020/selenium-webdriver-disabling-chrome-logging-messages/

Comments

2

You just need to find what logger is outputing the log to console then adjust it

`

    loggers = logging.Logger.manager.loggerDict

    # Print the names of all the loggers
    for name in loggers:
        print(name)
    logging.getLogger('urllib3.connectionpool').setLevel(logging.WARNING)
    logging.getLogger('selenium.webdriver.remote.remote_connection').setLevel(logging.WARNING)

`

Comments

1

if you set service_log_path = None, it won't generate the geckodriver.log file:

driver = webdriver.Firefox(options=options, service_log_path=None)

Comments

1

I know this is old but this is still the first thing that comes up when you search for a way to prevent logging from selenium and it did not get rid of the "dev listening" messages for me and I found a way that does:

ChromeDriverService service = ChromeDriverService.CreateDefaultService();
service.HideCommandPromptWindow = true;
IWebDriver driver = new ChromeDriver(service,options);

Comments

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.