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.
Community
  • 1
  • 1
Oddthinking
  • 24,359
  • 19
  • 83
  • 121
  • 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. – Oddthinking Sep 05 '14 at 03:49
  • Does this answer your question? [How to disable logging using Selenium with Python binding](https://stackoverflow.com/questions/11613869/how-to-disable-logging-using-selenium-with-python-binding) – architux Mar 06 '20 at 13:50

4 Answers4

59

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.

dan-klasson
  • 13,734
  • 14
  • 63
  • 101
alecxe
  • 462,703
  • 120
  • 1,088
  • 1,195
  • 2
    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. – uchuugaka Aug 30 '16 at 20:30
  • 4
    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!!! – uchuugaka Aug 30 '16 at 20:32
  • @uchuugaka glad it helped! – alecxe Aug 31 '16 at 00:25
  • it works and for some reason it is undocumented: https://seleniumhq.github.io/selenium/docs/api/py/webdriver_remote/selenium.webdriver.remote.remote_connection.html#module-selenium.webdriver.remote.remote_connection – robertspierre Jul 06 '17 at 08:58
  • 2
    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. – prm296 Nov 20 '19 at 05:25
29

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.

AntonG
  • 289
  • 3
  • 4
  • 1
    Your answer fully solved my case while others can't. – Lê Quang Duy Oct 12 '20 at 02:17
  • Removed all debug statements. Thanks – Roshan Khandelwal Nov 26 '20 at 13:03
  • 1
    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). – adrz Apr 19 '21 at 22:43
  • 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) – arquillos Sep 24 '21 at 10:34
  • The code runs but the messages are still displayed, even when I set level to "FATAL". Not sure why... – Yan King Yin Jan 18 '22 at 07:48
13
import logging
selenium_logger = logging.getLogger('selenium.webdriver.remote.remote_connection')
# Only display possible problems
selenium_logger.setLevel(logging.WARNING)
Andrew
  • 603
  • 1
  • 5
  • 13
  • 1
    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 @alecxe – uchuugaka Aug 30 '16 at 20:32
2

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.

Akendo
  • 2,299
  • 2
  • 18
  • 16
  • 1
    I'm also using logging.basicConfig, and have the same issue, Selenium uses the same and now its log messages find their way into my logs (but only for a logging priority of debug or higher). What do you mean you defined your own logger? As in, you didn't use Python's logging module and wrote your own logging solution? Perhaps you can also show some code samples? – Dennis Mar 09 '13 at 20:26