谈谈Selenium中的日志

谈谈Selenium中的日志

  • 来源于一位同学,“老师为啥firefox执行后会有日志文件,chrome没有呢?”

比对

  • 你打开chrome浏览器

    from selenium import webdriver
    driver = webdriver.Chrome()
    
  • 这样是没有日志的

  • 同样的代码,你打开firefox

    from selenium import webdriver
    driver = webdriver.Firefox()
    
  • 就会有个日志文件geckodriver.log生成,默认在你上面代码所在目录

  • 内容大致如下

    1678943199197	geckodriver	INFO	Listening on 127.0.0.1:8695
    1678943202290	mozrunner::runner	INFO	Running command: "C:\Program Files\Mozilla Firefox\firefox.exe" "--marionette" "--remote-debugging-port" "8696" "-no-remote" "-profile" "C:\Users\SONGQI~1\AppData\Local\Temp\rust_mozprofileNtWGR9"
    1678943202558	Marionette	INFO	Marionette enabled
    Dynamically enable window occlusion 0
    1678943202562	Marionette	INFO	Listening on port 8705
    WebDriver BiDi listening on ws://127.0.0.1:8696
    1678943202947	RemoteAgent	WARN	TLS certificate errors will be ignored for this session
    console.warn: SearchSettings: "get: No settings file exists, new profile?" (new NotFoundError("Could not open the file at C:\Users\SONGQI~1\AppData\Local\Temp\rust_mozprofileNtWGR9\search.json.mozlz4", (void 0)))
    DevTools listening on ws://127.0.0.1:8696/devtools/browser/791df03a-8db6-429b-969a-32cd077b3c2f
    
    
  • 的确是这样的,chrome和firefox2个浏览器在selenium中的api都是有差异的

  • 下面的代码可以让你看到2者独有的那些api

    from selenium import webdriver
    driver1 = webdriver.Chrome()
    driver2 = webdriver.Firefox()
    set_of_chrome_api = set([_ for _ in dir(driver1) if _[0]!='_'])
    set_of_firefox_api = set([_ for _ in dir(driver2) if _[0]!='_'])
    print('firefox特有的:',(set_of_chrome_api|set_of_firefox_api)-set_of_chrome_api)
    print('-------------')
    print('chrome特有的:',(set_of_chrome_api|set_of_firefox_api)-set_of_firefox_api)
    

让chrome也有日志

  • 首先来看下Chrome这个类的初始化参数都有哪些

    # seleniumwebdriverchromewebdriver.py
    class WebDriver(ChromiumDriver):
        def __init__(self, executable_path=DEFAULT_EXECUTABLE_PATH, port=DEFAULT_PORT,
                     options: Options = None, service_args=None,
                     desired_capabilities=None, service_log_path=DEFAULT_SERVICE_LOG_PATH,
                     chrome_options=None, service: Service = None, keep_alive=DEFAULT_KEEP_ALIVE):
    
  • 你应该一眼就看到有日志相关的参数了service_log_path

  • 有个默认值DEFAULT_SERVICE_LOG_PATH

  • 往上能看到定义:DEFAULT_SERVICE_LOG_PATH = None

  • 所以chrome就没有日志了

  • 要有就简单了,给一个值就行了,这样

    from selenium import webdriver
    driver = webdriver.Chrome(service_log_path='chrome.log')
    
  • 妥妥的在当前目录下生成了一个chrome.log文件里面也记录着本次操作的日志

  • 多说几句,默认的日志就记录到INFO/WARN这样的级别

  • 如果要加DEBUG的信息,可以这样

    from selenium import webdriver
    driver = webdriver.Chrome(service_args=['--verbose'],
                              service_log_path='chrome.log')
    
    

让firefox没有日志,没那么简单

  • 循着刚才的思路,让firefox没有日志似乎就很简单了

  • 依葫芦画瓢,找到Firefox的定义

    # seleniumwebdriverfirefoxwebdriver.py
    # 注意,此处的路径跟上面的chrome的并不一样
    class WebDriver(RemoteWebDriver):
        def __init__(self, firefox_profile=None, firefox_binary=None,
                     capabilities=None, proxy=None,
                     executable_path=DEFAULT_EXECUTABLE_PATH, options=None,
                     service_log_path=DEFAULT_SERVICE_LOG_PATH,
                     service_args=None, service=None, desired_capabilities=None,
                     log_path=DEFAULT_LOG_PATH, keep_alive=True):
    
  • 看到了参数service_log_path,一样也有默认值DEFAULT_SERVICE_LOG_PATH

  • 找到定义处,DEFAULT_SERVICE_LOG_PATH = "geckodriver.log"

  • 改为None试试

    from selenium import webdriver
    driver = webdriver.Firefox(service_log_path=None)
    
  • 效果有,但又有点不对

  • 控制台冒出了很多信息

    demo_log.py:2: DeprecationWarning: service_log_path has been deprecated, please pass in a Service object
      driver = webdriver.Firefox(service_log_path=None)
    1678944678241	geckodriver	INFO	Listening on 127.0.0.1:10729
    1678944681310	mozrunner::runner	INFO	Running command: "C:\Program Files\Mozilla Firefox\firefox.exe" "--marionette" "--remote-debugging-port" "10730" "-no-remote" "-profile" "C:\Users\SONGQI~1\AppData\Local\Temp\rust_mozprofileeEN3y9"
    Dynamically enable window occlusion 0
    1678944681583	Marionette	INFO	Marionette enabled
    1678944681587	Marionette	INFO	Listening on port 10743
    WebDriver BiDi listening on ws://127.0.0.1:10730
    1678944681883	RemoteAgent	WARN	TLS certificate errors will be ignored for this session
    console.warn: SearchSettings: "get: No settings file exists, new profile?" (new NotFoundError("Could not open the file at C:\Users\SONGQI~1\AppData\Local\Temp\rust_mozprofileeEN3y9\search.json.mozlz4", (void 0)))
    DevTools listening on ws://127.0.0.1:10730/devtools/browser/a4f3a501-e642-4540-8998-ae1ea0b3abc5
    
  • 仔细一看,跟之前在geckodriver.log中打印的类似

  • 还多了一句

    DeprecationWarning: service_log_path has been deprecated, please pass in a Service object
    
  • 原来service_log_path被弃用了,需要传递一个Service对象

  • 怎么传递Service对象呢?找到其定义,注意路径

  • 有多个Service在selenium这个库中,你要的是firefox下的

    #  seleniumwebdriverfirefoxservice.py
    class Service(service.Service):
        """Object that manages the starting and stopping of the
        GeckoDriver."""
    
        def __init__(self, executable_path: str = DEFAULT_EXECUTABLE_PATH,
                     port: int = 0, service_args: List[str] = None,
                     log_path: str = "geckodriver.log", env: dict = None):
    
  • 可以看到,这里有个参数log_path,其默认值是"geckodriver.log"

  • 那我们就可以这样修改了

    from selenium import webdriver
    from selenium.webdriver.firefox.service import Service
    # 听你的,传递一个Service对象
    # Service(log_path='') 就是一个
    driver = webdriver.Firefox(service=Service(log_path=''))
    
  • 至此DeprecationWarning: service_log_path has been deprecated, please pass in a Service object的这段没了

  • 不过下面的1678945260755 geckodriver INFO Listening on 127.0.0.1:11634,这样的仍然存在的

  • 而这部分实际是geckodriver这个驱动的打印信息

    C:Usersxxx>geckodriver
    1678945567307   geckodriver     INFO    Listening on 127.0.0.1:4444
    
  • 如果要去掉,就是让geckodriver这个程序静默

  • 没有找到太好的办法,geckodriver的帮助如下

    C:Usersxxx>geckodriver --help
    geckodriver 0.30.0 (d372710b98a6 2021-09-16 10:29 +0300)
    WebDriver implementation for Firefox
    
    USAGE:
        geckodriver [FLAGS] [OPTIONS]
    
    FLAGS:
            --connect-existing    Connect to an existing Firefox instance
        -h, --help                Prints this message
            --jsdebugger          Attach browser toolbox debugger for Firefox
        -v                        Log level verbosity (-v for debug and -vv for trace level)
        -V, --version             Prints version and copying information
    
    OPTIONS:
            --android-storage     Selects storage location to be used for test data (deprecated). [possible
                                                   values: auto, app, internal, sdcard]
        -b, --binary                       Path to the Firefox binary
            --log                           Set Gecko log level [possible values: fatal, error, warn, info, config,
                                                   debug, trace]
            --marionette-host                Host to use to connect to Gecko [default: 127.0.0.1]
            --marionette-port                Port to use to connect to Gecko [default: system-allocated port]
            --host                           Host IP to use for WebDriver server [default: 127.0.0.1]
        -p, --port                           Port to use for WebDriver server [default: 4444]
            --websocket-port                 Port to use to connect to WebDriver BiDi [default: 9222]
    
  • --log 这个参数可以让它控制输出级别

  • 最终我们可以这样写

    from selenium import webdriver
    from selenium.webdriver.firefox.service import Service
    
    driver = webdriver.Firefox(service=Service(log_path='',
                        service_args=['--log','fatal']))  # 你不能 ['--log fatal'] 会报错
    
    
  • 然,即便如此,控制台一样有打印

    Dynamically enable window occlusion 0
    WebDriver BiDi listening on ws://127.0.0.1:12166
    console.warn: SearchSettings: "get: No settings file exists, new profile?" (new NotFoundError("Could not open the file at C:\Users\SONGQI~1\AppData\Local\Temp\rust_mozprofiledrxhm5\search.json.mozlz4", (void 0)))
    DevTools listening on ws://127.0.0.1:12166/devtools/browser/22a37523-84bc-4bd6-a5e9-79c30825d1e8
    
  • 啊~~~~~~~~~无语了,不弄了,放弃了,干点有意义的事情吧,有点无趣

文章来源于互联网:谈谈Selenium中的日志

THE END
分享
二维码