欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

封装查找元素以及集成日志输出,Base模块

程序员文章站 2022-05-03 22:13:43
关于查找元素封装,Base模块 ......

关于查找元素封装,base模块

"""
selenium 封装基类

封装查找元素以及集成日志输出
"""

from htmlreport import logger
from selenium import webdriver
from selenium.webdriver.remote.webelement import webelement


class base(object):
    def __init__(self, driver: webdriver.remote = none):
        self.driver = driver
        self.logger = logger()

    def find_element(self, locator: tuple) -> webelement:
        """查找单个元素

        :param locator: 定位器(by=by.id, value=none)
        :return: webelement
        """
        self.logger.info(f"查找元素:{locator}")
        try:
            element = self.driver.find_element(*locator)
        except exception:
            self.logger.info("查找元素失败")
            raise
        else:
            self.logger.info("查找元素成功")
        return element

    def find_elements(self, locator: tuple):
        """查找元素集合

        :param locator: 定位器(by=by.id, value=none)
        :return: 元素对象集合
        :rtype: list of webelement
        """
        self.logger.info(f"查找元素集合:{locator}")
        elements = self.driver.find_elements(*locator)
        self.logger.info(f"查找到{len(elements)}个元素")
        return elements