谈谈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这个类的初始化参数都有哪些

    # selenium\webdriver\chrome\webdriver.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的定义

    # selenium\webdriver\firefox\webdriver.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下的

    #  selenium\webdriver\firefox\service.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:\Users\xxx>geckodriver
    1678945567307 geckodriver INFO Listening on 127.0.0.1:4444
  • 如果要去掉,就是让geckodriver这个程序静默

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

    C:\Users\xxx>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 <ANDROID_STORAGE> Selects storage location to be used for test data (deprecated). [possible
    values: auto, app, internal, sdcard]
    -b, --binary <BINARY> Path to the Firefox binary
    --log <LEVEL> Set Gecko log level [possible values: fatal, error, warn, info, config,
    debug, trace]
    --marionette-host <HOST> Host to use to connect to Gecko [default: 127.0.0.1]
    --marionette-port <PORT> Port to use to connect to Gecko [default: system-allocated port]
    --host <HOST> Host IP to use for WebDriver server [default: 127.0.0.1]
    -p, --port <PORT> Port to use for WebDriver server [default: 4444]
    --websocket-port <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中的日志的更多相关文章

  1. selenium===selenium自动化添加日志(转)

    本文转自 selenium自动化添加日志 于logging日志的介绍,主要有两大功能,一个是控制台的输出,一个是保存到本地文件 先封装logging模块,保存到common文件夹命名为logger.p ...

  2. 在 Selenium 中让 PhantomJS 执行它的 API

    from selenium import webdriver driver = webdriver.PhantomJS() script = "var page = this; page.o ...

  3. 谈谈TCP中的TIME_WAIT

    所以,本文也来凑个热闹,来谈谈TIME_WAIT. 为什么要有TIME_WAIT? TIME_WAIT是TCP主动关闭连接一方的一个状态,TCP断开连接的时序图如下: 当主动断开连接的一方(Initi ...

  4. .NetCore中的日志(2)集成第三方日志工具

    .NetCore中的日志(2)集成第三方日志工具 0x00 在.NetCore的Logging组件中集成NLog 上一篇讨论了.NetCore中日志框架的结构,这一篇讨论一下.NetCore的Logg ...

  5. .NetCore中的日志(1)日志组件解析

    .NetCore中的日志(1)日志组件解析 0x00 问题的产生 日志记录功能在开发中很常用,可以记录程序运行的细节,也可以记录用户的行为.在之前开发时我一般都是用自己写的小工具来记录日志,输出目标包 ...

  6. Laravel中的日志与上传

    PHP中的框架众多,我自己就接触了好几个.大学那会啥也不懂啥也不会,拿了一个ThinkPHP学了.也许有好多人吐槽TP,但是个人感觉不能说哪个框架好,哪个框架不好,再不好的框架你能把源码读上一遍,框架 ...

  7. Selenium中的几种等待方式,需特别注意implicitlyWait的用法

    摘:http://blog.csdn.net/pf20050904/article/details/20052485 最近在项目过程中使用selenium 判断元素是否存在的时候 遇到一个很坑爹的问题 ...

  8. PHP框架中的日志系统

    现在在一家公司做PHP后台开发程序猿(我们组没有前端,做活动时会做前端的东西),刚开始到公司的时候花2个周赶出了一个前端加后台的活动(记得当时做不出来周末加了两天班...),到现在过去4个多月了,可以 ...

  9. mysql的innodb中事务日志ib_logfile

    mysql的innodb中事务日志ib_logfile事务日志或称redo日志,在mysql中默认以ib_logfile0,ib_logfile1名称存在,可以手工修改参数,调节开启几组日志来服务于当 ...

  10. 传递给数据库 'master' 中的日志扫描操作的日志扫描号无效

    错误:连接数据库的时候提示:SQL Server 检测到基于一致性的逻辑 I/O 错误 校验和不正确 C:\Documents and Settings\Administrator>" ...

随机推荐

  1. 【Qt】汇总Qt坑若干

    1..pro里添加了类以后调用时还是报错? solution:注意添加或者修改以后要保存保存保存,否则Qt是不会自己更新的! 2.在ui界面修改了类的成员名,保存了以后.cpp里调用,成员名还是不更新 ...

  2. ZSTUOJ刷题⑧:Problem G.--铺地砖

    Problem G: 铺地砖 Time Limit: 1 Sec  Memory Limit: 64 MBSubmit: 6461  Solved: 2592 Description 元旦过去了,新年 ...

  3. WSL2与ensp的40故障

    在使用ensp做radius认证的时候看到了Linux平台的freeradius认证服务器,于是使用了Windows平台的sub system: WSL2,按照网上的教程安装,并且安装了docker ...

  4. 浏览器输入URL发生了什么:DNS解析、TCP握手、HTTP缓存、重定向、服务器状态码、渲染引擎和JS引擎互斥、渲染过程、浏览器进程、网络进程、渲染进程

    输入地址,浏览器查找域名的 IP 地址. 浏览器向 该 IP 地址的web 服务器发送一个 HTTP 请求, 在发送请求之前浏览器和服务器建立TCP的三次握手,判断是否是HTTP缓存, 如果是强制缓存 ...

  5. django前后端分离接口开发

    用django进行接口开发 接口: /api/parameter (同一个接口地址实现不同的请求方式) 全局参数(get,post,put,delete) GET:获取全局参数的所有数据 POST:创 ...

  6. loadrunner 11

    1  环境windows server 2012  2019 的.net 时, 服务器管理器--仪表板--勾选.net后报错? 其中一个办法就是将服务器临时联网,就能安装成功. 2   联机时 提示如 ...

  7. pnn模型 待整理

    https://blog.csdn.net/qq_18293213/article/details/90262378?spm=1001.2101.3001.6650.5&utm_medium= ...

  8. c# 一些方法记录

    // 返回当前目录的路径 fileName = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "当前目录下的一个文件"); ...

  9. Kicad使用技巧--原理图复用

    平时主要使用AD.kicad和立创eda,最近在琢磨一下各个软件的使用技巧,提升一下画图效率,首先想到的就是原理图复用,我想象中的原理图复用应该像AutoCAD的图块功能一样. 这次先说kicad,摸 ...

  10. css卡片样式

    .view-1-user-card { margin: 20px 5% 10px 5%; height: 124px; width: 90%; background: linear-gradient( ...