关闭Selenium中的日志记录(来自Python)

Odd*_*ing 23 python selenium selenium-webdriver

我最近继承了一些用Python 2.7编写的Selenium Webdriver代码.它正在将大量数据记录到Ubuntu上的/ tmp中 - 以至于它正成为一个问题.我试图将其关闭(或者至少关闭).

我一直在尝试使用RTFM,但这是Selenium(2.19.0)的新版本,手册尚未编写!

我可以看到有一种set_browser_log_level(logLevel)声音很有前途的方法,但要实现它,我需要实例化一个selenium.selenium.selenium对象.我不需要实例化其中一个,它需要很多参数(主机?什么端口?),我不希望提供.

显然,我误解了一些事情.

有人可以解释一下(a)如何关闭日志记录,或者(b)selenium.selenium.selenium.selenium.selenium(我可能已经被带走了,抱歉!)想要与之交谈的服务是什么?


相关问题:在Selenium中,如何关闭日志记录? 这是Selenium的旧版本,我相信它是用脚本语言调用的.

ale*_*cxe 36

这是帮助我克服这个问题的原因:

import logging
from selenium.webdriver.remote.remote_connection import LOGGER
LOGGER.setLevel(logging.WARNING)
Run Code Online (Sandbox Code Playgroud)

注意:此代码应在webdriver初始化之前放置.

希望有所帮助.

  • 这是正确的答案.上面接受的答案依赖于日志中显示的命名空间,而不是日志记录模块的工作方式. (2认同)
  • 我将我的代码修改为“从selenium.webdriver.remote.remote_connection import LOGGER作为serverLogger”,以便我可以根据需要与代码分开设置级别。谢谢!!! (2认同)
  • 请参考@AntonG提供的答案,因为您还需要更改 urllib3 的记录器级别以完全抑制 Selenium 生成的调试消息。 (2认同)

小智 16

来自 alecxe 的回答对我有用。然而,日志中仍有一些调试消息,源自 urllib3。它是由硒导入的,不受上述解决方案的影响。这是我使用的,它的价值:

# 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)
Run Code Online (Sandbox Code Playgroud)

如果有人知道实现相同目标的更多 pythonic 方法 - 我会很高兴听到它。


And*_*rew 11

import logging
selenium_logger = logging.getLogger('selenium.webdriver.remote.remote_connection')
# Only display possible problems
selenium_logger.setLevel(logging.WARNING)
Run Code Online (Sandbox Code Playgroud)

  • 对我没有帮助. (2认同)