【问题标题】:How to disable logging using Selenium with Python binding如何使用 Selenium 和 Python 绑定禁用日志记录
【发布时间】:2012-07-23 13:49:41
【问题描述】:

简单问题:如何在 Python 绑定中使用 Selenium 时完全禁用日志记录,前代码如下:

browser = webdriver.Chrome()

我尝试过类似的方法:

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

甚至:

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

但文件“chromedriver.log”仍然出现在每次新的测试运行中。

【问题讨论】:

    标签: python selenium-chromedriver


    【解决方案1】:

    您可以为 Chrome 浏览器设置 options.add_argument("--log-level=3") 以使用 Selenuim 运行,或者您可以将日志记录级别设置为更高的级别:

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

    但在这种情况下无论如何都会出现一些消息,包括启动 DevTools 消息或 SSL 握手错误消息。

    要在控制台中以完全静默模式运行带有 Selenium 的 Chrome 浏览器,您应该使用这个 sn-p:

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

    该技巧将抑制来自 Selenium 驱动程序或浏览器本身的任何控制台消息,包括一开始的第一条消息 DevTools listening on ws://127.0.0.1

    同时,一些运行时的分步数据可以保存到服务日志文件,以防它的参数被添加。

    【讨论】:

      【解决方案2】:
      driver = webdriver.Chrome(service_log_path='/dev/null')
      

      【讨论】:

        【解决方案3】:

        仅适用于 Windows 用户的示例:

        webdriver.Firefox(log_path='NUL')
        

        接受的答案是正确的,但如果你像我一样是 Python/Windows 的新手,这样的例子会减少你几个小时的谷歌时间。

        【讨论】:

        • 我还没有测试过这个,但我很确定你可以使用 logpath=os.devnull 并且它将独立于操作系统工作。 (当然需要导入os)。
        • 已经测试了这个,它可以工作。我得到了数 GB 的日志,它正在扼杀我们的服务器。现在,什么都没有。谢谢@codingatty
        • 在我的机器上不工作(Manjaro 最新)。尝试了os.devnullNUL。我的代码是这个driver = webdriver.Firefox(log_path=os.devnull, options=options, capabilities=capabilities, firefox_profile=profile)
        【解决方案4】:

        Chrome 网络驱动程序的source code 显示存在一个名为service_log_path 的选项。

        所以如果你想摆脱文件,你可以设置这个属性为

        • /dev/null 如果你在 Linux/Unix 下运行;
        • NULwindows下

        希望对你有帮助

        【讨论】:

          【解决方案5】:

          这对我有用:

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

          礼貌:

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

          【讨论】:

            【解决方案6】:

            如果设置 service_log_path = None,则不会生成 geckodriver.log 文件:

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

            【讨论】:

              【解决方案7】:

              我知道这已经过时了,但这仍然是当您寻找一种防止硒记录的方法时出现的第一件事,它并没有为我消除“开发监听”消息,我找到了一种方法确实:

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

              【讨论】:

                【解决方案8】:

                要使用 SeleniumPython 禁用日志记录,您需要通过 ChromeOptions() 的实例添加一个实验选项em>如下:

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

                实施

                兼容代码

                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)
                

                【讨论】:

                  猜你喜欢
                  • 2014-07-16
                  • 1970-01-01
                  • 1970-01-01
                  • 1970-01-01
                  • 2015-02-23
                  • 2018-12-24
                  • 2020-01-17
                  • 2016-07-04
                  • 1970-01-01
                  相关资源
                  最近更新 更多