Turning off logging in Selenium (from Python)
Asked Answered
S

4

37

I've recently inherited some Selenium Webdriver code, written in Python 2.7. It is logging copious amounts of data to /tmp on Ubuntu - so much that it is becoming a problem. I am trying to turn it off (or at least down).

I have been running around trying to RTFM, but this is a new version of Selenium (2.19.0) and the manuals aren't written yet!

I can see there is a method called set_browser_log_level(logLevel) which sounds promising, but to get to it, I need to instantiate a selenium.selenium.selenium object. I don't otherwise have to instantiate one of these, and it takes a lot of parameters (which host? what port?) that I am not expecting to have to provide.

Clearly, I am misunderstanding something.

Can someone please explain either (a) how to turn off logging, or (b) what service is it that selenium.selenium.selenium.selenium.selenium (I may have got carried away there, sorry!) wants to talk to?


Related question: In Selenium, how do I turn off logging? This is an older version of Selenium, and calling it from the scripting language, I believe.
Sulphurbottom answered 10/2, 2012 at 10:46 Comment(3)
Do you use some logger? I had a very similar issue? i used a simple logging.basicConfig, but Selenium does this as well. My solution was to define my own logger. Maybe you print some Code samples.Boost
I didn't mention: One hack solution I used was to create an empty file of the same name in the temp directory, and remove permissions. Selenium still worked but didn't log.Sulphurbottom
Does this answer your question? How to disable logging using Selenium with Python bindingBegonia
W
62

Here's what helped me to overcome the problem:

import logging
from selenium.webdriver.remote.remote_connection import LOGGER
LOGGER.setLevel(logging.WARNING)

Note: this code should be put before webdriver initialization.

Hope that helps.

Whalebone answered 10/2, 2014 at 22:6 Comment(5)
THIS is the right answer. The above, accepted answer is relying on what shows up in the logs as the namespace, not how the logging module works.Ovid
I modified mine to be from selenium.webdriver.remote.remote_connection import LOGGER as serverLogger so that I can set the level separately from my code as needed. Thanks!!!Ovid
@Ovid glad it helped!Whalebone
it works and for some reason it is undocumented: seleniumhq.github.io/selenium/docs/api/py/webdriver_remote/…Sapele
Please refer to the the answer provided by @AntonG, since you also need to change urllib3's logger level in order to fully suppress debug messages generated by Selenium.Lactase
N
31

The answer from alecxe worked for me. There were still some debug messages in the log however, originating from urllib3. It is imported by selenium, and not affected by the solution above. Here is what I used, for what it's worth:

# Set the threshold for selenium to WARNING
from selenium.webdriver.remote.remote_connection import LOGGER as seleniumLogger
seleniumLogger.setLevel(logging.WARNING)
# Set the threshold for urllib3 to WARNING
from urllib3.connectionpool import log as urllibLogger
urllibLogger.setLevel(logging.WARNING)

If someone knows of more pythonic way to achieve the same - I'll be glad to hear it.

Nightrider answered 22/5, 2019 at 14:49 Comment(5)
Your answer fully solved my case while others can't.Ramsden
Removed all debug statements. ThanksFarrel
Indeed, there is a more Pythonic way to achieve it - instead of importing loggers from particular libraries, you can do it just with the standard logging interface - import logging; logging.getLogger("urllib3").setLevel(logging.WARNING) (the same of course works for Selenium or any other library).Polecat
It worked for me! thank you so much. I ended up using the "pythonic" way: import logging # Set the threshold for selenium to WARNING logging.getLogger("selenium").setLevel(logging.WARNING) # Set the threshold for urllib3 to WARNING logging.getLogger("urllib3").setLevel(logging.WARNING)Caryopsis
The code runs but the messages are still displayed, even when I set level to "FATAL". Not sure why...Holtorf
N
13
import logging
selenium_logger = logging.getLogger('selenium.webdriver.remote.remote_connection')
# Only display possible problems
selenium_logger.setLevel(logging.WARNING)
Nodal answered 14/3, 2013 at 2:52 Comment(2)
This is wrong because you are hard coding the namespace and that may not apply to everyone's setup. It all depends on how and where things were imported. See the answer by @WhaleboneOvid
What I like about this answer is that I can quickly silence a logger without having to dig into the code of the module.Monophony
F
0

You can fine-tune it, this will turn of all logging, but the print will let you see what module you affects:

import logging
for logger in [logging.getLogger(name) for name in logging.root.manager.loggerDict]:
    # print(logger)
    logger.setLevel(logging.WARNING)
    #logger.setlevel(logging.DEBUG)
Folsom answered 13/9, 2023 at 6:57 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.