如何使用Selenium Python在文本文件中记录错误消息
我正在使用Pytest框架进行自动化网站。我已经编写了以下代码
来
2022-04-20 14:35:45,015 - BasePage.py:[27] - [INFO] - Clicking on an Element health_XPATH
记录
import logging
import time
from Utilities.filepath import *
class Logger():
def __init__(self, logger, file_level=logging.INFO):
self.logger = logging.getLogger(logger)
self.logger.setLevel(logging.DEBUG)
fmt = logging.Formatter('%(asctime)s -'
' %(filename)s:[%(lineno)s] - [%(levelname)s] - %(message)s')
curr_time = time.strftime("%Y-%m-%d")
# self.LogFileName = 'C:\\Users\\aprat\\PycharmProjects\\Regression-Web-Automation\Logs\\log' + curr_time + '.txt'
self.LogFileName = FilePath.logfile_path + curr_time + '.txt'
fh = logging.FileHandler(self.LogFileName, mode="a")
fh.setFormatter(fmt)
fh.setLevel(file_level)
self.logger.addHandler(fh)
错误日志(文本文件)。仅从控制台可以看到错误消息。
我的目标是捕获我上面提到的所有执行步骤,以及诸如“找不到元素”或“陈旧元素异常”等错误消息。
I am using pytest framework for automation a website. I have written the below code for logging the errors but this code only captures execution steps of the automation scripts like below
Logfile Example
2022-04-20 14:35:45,015 - BasePage.py:[27] - [INFO] - Clicking on an Element health_XPATH
Loggin Code:
import logging
import time
from Utilities.filepath import *
class Logger():
def __init__(self, logger, file_level=logging.INFO):
self.logger = logging.getLogger(logger)
self.logger.setLevel(logging.DEBUG)
fmt = logging.Formatter('%(asctime)s -'
' %(filename)s:[%(lineno)s] - [%(levelname)s] - %(message)s')
curr_time = time.strftime("%Y-%m-%d")
# self.LogFileName = 'C:\\Users\\aprat\\PycharmProjects\\Regression-Web-Automation\Logs\\log' + curr_time + '.txt'
self.LogFileName = FilePath.logfile_path + curr_time + '.txt'
fh = logging.FileHandler(self.LogFileName, mode="a")
fh.setFormatter(fmt)
fh.setLevel(file_level)
self.logger.addHandler(fh)
Now for any error occurs in case of any failures or script issues, that error message is not getting captured in the log(text file). The error message is only can be seen from the console.
My goal is to capture all the execution steps as I have mentioned above and also the error messages like "element not found" or "stale element exception" etc.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
使用 loguru 而不是?它几乎是开箱即用的,
您可以很容易地发现异常(从loguru 示例)
What about using loguru instead?. it is pretty much out of the box
you can catch easily exceptions (from loguru examples)