谈谈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这个类的初始化参数都有哪些
# 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中的日志的更多相关文章
- selenium===selenium自动化添加日志(转)
本文转自 selenium自动化添加日志 于logging日志的介绍,主要有两大功能,一个是控制台的输出,一个是保存到本地文件 先封装logging模块,保存到common文件夹命名为logger.p ...
- 在 Selenium 中让 PhantomJS 执行它的 API
from selenium import webdriver driver = webdriver.PhantomJS() script = "var page = this; page.o ...
- 谈谈TCP中的TIME_WAIT
所以,本文也来凑个热闹,来谈谈TIME_WAIT. 为什么要有TIME_WAIT? TIME_WAIT是TCP主动关闭连接一方的一个状态,TCP断开连接的时序图如下: 当主动断开连接的一方(Initi ...
- .NetCore中的日志(2)集成第三方日志工具
.NetCore中的日志(2)集成第三方日志工具 0x00 在.NetCore的Logging组件中集成NLog 上一篇讨论了.NetCore中日志框架的结构,这一篇讨论一下.NetCore的Logg ...
- .NetCore中的日志(1)日志组件解析
.NetCore中的日志(1)日志组件解析 0x00 问题的产生 日志记录功能在开发中很常用,可以记录程序运行的细节,也可以记录用户的行为.在之前开发时我一般都是用自己写的小工具来记录日志,输出目标包 ...
- Laravel中的日志与上传
PHP中的框架众多,我自己就接触了好几个.大学那会啥也不懂啥也不会,拿了一个ThinkPHP学了.也许有好多人吐槽TP,但是个人感觉不能说哪个框架好,哪个框架不好,再不好的框架你能把源码读上一遍,框架 ...
- Selenium中的几种等待方式,需特别注意implicitlyWait的用法
摘:http://blog.csdn.net/pf20050904/article/details/20052485 最近在项目过程中使用selenium 判断元素是否存在的时候 遇到一个很坑爹的问题 ...
- PHP框架中的日志系统
现在在一家公司做PHP后台开发程序猿(我们组没有前端,做活动时会做前端的东西),刚开始到公司的时候花2个周赶出了一个前端加后台的活动(记得当时做不出来周末加了两天班...),到现在过去4个多月了,可以 ...
- mysql的innodb中事务日志ib_logfile
mysql的innodb中事务日志ib_logfile事务日志或称redo日志,在mysql中默认以ib_logfile0,ib_logfile1名称存在,可以手工修改参数,调节开启几组日志来服务于当 ...
- 传递给数据库 'master' 中的日志扫描操作的日志扫描号无效
错误:连接数据库的时候提示:SQL Server 检测到基于一致性的逻辑 I/O 错误 校验和不正确 C:\Documents and Settings\Administrator>" ...
随机推荐
- Centos7.5下安装nginx
#cd /usr/local #wget http://nginx.org/download/nginx-1.8.0.tar.gz #tar -xzvf nginx-1.8.0.tar.gz #cd ...
- [转]Selenium私房菜系列1 -- Selenium简介
一.Selenium是什么? Selenium是ThroughtWorks公司一个强大的开源Web功能测试工具系列,本系列现在主要包括以下4款: 1.Selenium Core:支持DHTML的测试案 ...
- JRAT远控
JRAT java写的一款远控 可以控制mac os Windows linux 系统 https://share.weiyun.com/21b56e3e5cab4b3f145d7c2330d107 ...
- C# 俩个字符串数据进行对比取出不同的数据
string sel_JD = "电冰箱,电热水壶,客厅空调,饮水机,"; string Operation_JD = "电冰箱,电热水壶,电动汽车,"; Li ...
- ubuntu安装samba服务
第一步 sudo apt-get install samba samba-common 安装完成 第二步 建立一个文件夹作为共享目录 sudo mkdir /home/yz/my_samba my_ ...
- vue富文本(5版本)组件
<template> <div> <div style="border: 1px solid #ccc; width: 500px"> < ...
- mybatis插入批量数据
1 for循环重复调用dao,消耗大 2 sql语句编写插入多条,只用于少数 3 mybatis的batch插入 @Test public void testInsertBatch2() ...
- ping 的七种用法【搬运】
原作者:Pheenet菲尼特 原地址:https://www.toutiao.com/a6783191796659782148/?tt_from=weixin&utm_campaign=cli ...
- IO在指定目录建文件
path= "d:\\Project\\log\\log.txt"; File file = new File(path); //创建文件目录 if(!file.getParent ...
- DHCP分配IP的流程
1.DHCP客户端以广播的形式发送DHCP Discover报文 2.所有的DHCP服务端都可以接收到这个DHCP Discover报文,所有的DHCP服务端都会给出响应,向DCHP客户端发送一个DH ...