谈谈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. TCP三次握手和四次挥手的原因所在

    报文从运用层传送到运输层,运输层通过TCP三次握手和服务器建立连接,四次挥手释放连接. 为什么需要三次握手呢?为了防止已失效的连接请求报文段突然又传送到了服务端,因而产生错误. 比如:client发出 ...

  2. 微信字体大小调整导致的H5页面错乱问题处理

    当用户调整微信字体大小时会导致H5页面错乱,解决方案如下: ios:在css中加入-webkit-text-size-adjust: 100% !important;   body {   -webk ...

  3. Linux非正式学习随笔(1)

    11.5进linux学的第一件事,找个中文输入法.Linux是一套免费的类unix操作系统GPL:gnu通用公共许可证.托马斯斯托曼提出gnu计划,自由软件思想的一个协议.Linux诞生1991年10 ...

  4. 等级保护2.0 三级-Linux 测评指导书

    等级保护2.0 三级-Linux 测评指导书 1.1安全计算环境 1.1.1身份鉴别        1.1.2访问控制        1.1.4入侵防范       1.1.5恶意代码防范      ...

  5. SpringBoot写第一个接口

    服务可以理解为一个接口,一个controller,一个做业务请求的 新建一个HelloWorldController import org.springframework.boot.SpringApp ...

  6. SpringBoot怎么管理封装java包的关系

    首先SpringBoot直接写注解加依赖就可以了,基本上不用写xml,非常方便,在这里我只写了两个核心包 为什么选择jar类型? SpringBoot基本上是个应用程序了,只要用java命令程序去运行 ...

  7. vue 封装时间格式化和number精确度

    //format.js 公用js /** * Parse the time to string * @param {(Object|string|number)} time * @param {str ...

  8. 微信小程序:流程/步骤流/时间轴自定义组件

    效果图: 1.首先在小程序components目录下新建一个名为step的文件夹,再建step组件名.结构如下. 直接上代码 step.wxml <view class="step&q ...

  9. ORA-00972: identifier is too long异常处理

    环境:由于数据库更换,做数据同步,提示 too long 问题,导致一直无法同步完数据. 经排查 oracle 历史数据库版本: Oracle Database 12c Standard Editio ...

  10. Vue router前端路由配置以及实现tab切换

    vue router 安装:npm install vue-router或cnpm install vue-router或yarn add vue-router. 安装完成之后会在package.js ...