How to disable logging using Selenium with Python binding
Asked Answered
P

9

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.

Plater answered 23/7, 2012 at 13:49 Comment(0)
A
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

Anny answered 6/2, 2013 at 13:56 Comment(0)
S
27

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.

Sudarium answered 6/3, 2020 at 13:26 Comment(1)
What about Edgdriver?Shorttempered
A
13
driver = webdriver.Chrome(service_log_path='/dev/null')
Anthropophagite answered 12/3, 2013 at 7:23 Comment(0)
A
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

Anny answered 6/2, 2013 at 13:56 Comment(0)
W
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.

Wolff answered 19/10, 2018 at 17:16 Comment(3)
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).Hauge
I have tested this, and it works. I was getting multi-gigabyte logs and it was killing our servers. Now, nothing at all. Thanks @HaugeArchaeornis
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)Ole
B
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)
Brooklyn answered 16/3, 2022 at 21:34 Comment(0)
W
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/

Wringer answered 17/2, 2021 at 14:48 Comment(0)
P
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)
Postfree answered 25/9, 2021 at 6:45 Comment(0)
G
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);
Gypsum answered 8/12, 2021 at 12:35 Comment(0)
S
1

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)

`

Semimonthly answered 16/6, 2023 at 3:56 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.