如何使用Selenium Python在文本文件中记录错误消息

发布于 2025-01-22 18:10:50 字数 1053 浏览 0 评论 0原文

我正在使用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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

倚栏听风 2025-01-29 18:10:50

使用 loguru 而不是?它几乎是开箱即用的,

您可以很容易地发现异常(从loguru 示例

@logger.catch
def my_function(x, y, z):
    # An error? It's caught anyway!
    return 1 / (x + y + z)

What about using loguru instead?. it is pretty much out of the box

you can catch easily exceptions (from loguru examples)

@logger.catch
def my_function(x, y, z):
    # An error? It's caught anyway!
    return 1 / (x + y + z)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文