Python 封装selenium元素定位FindElement类

Python 封装selenium元素定位FindElement类,第1张

概述#coding=utf-8fromconfig.setting_baseimportSettingBasefromutil.read_iniimportReadInifromselenium.webdriver.common.byimportByfromselenium.webdriver.support.waitimportWebDriverWaitfromselenium.webdriver.supportimportexpected_conditionsa
# Coding=utf-8from config.setting_base import SettingBasefrom util.read_ini import ReadInifrom selenium.webdriver.common.by import Byfrom selenium.webdriver.support.wait import webdriverwaitfrom selenium.webdriver.support import expected_conditions as ESfrom contextlib import contextmanagerclass FindElement(object):    def __init__(self, driver, file_name=None, node=None):        self.driver = driver        self.read_ini = ReadIni(file_name=file_name, node=node)        self.locator = ()# 配置文件中定位元素的方式,如:ID,name,xpath    def get_by_key(self, key):        data = self.read_ini.get_value(key)        by_key = data.split('>')[0].strip()        return by_key    # 配置文件的value    def get_value(self, key):        data = self.read_ini.get_value(key)        value = data.split('>')[1].strip()        return valuedef selector_to_locator(self, key):        selector_by = self.get_by_key(key)        selector_value = self.get_value(key)        if selector_by == 'ID':            locator = (By.ID, selector_value)        elif selector_by == 'name':            locator = (By.name, selector_value)        elif selector_by == 'class_name':            locator = (By.CLASS_name, selector_value)        elif selector_by == 'link_text':            locator = (By.PARTIAL_link_TEXT, selector_value)        elif selector_by == 'tag_name':            locator = (By.TAG_name, selector_value)        elif selector_by == 'xpath':            locator = (By.XPATH, selector_value)        elif selector_by == 'CSS_selector':            locator = (By.CSS_SELECTOR, selector_value)        else:            raise nameError("Please enter a valID selector of targeting elements.")        return locator    @contextmanager    def find_base(self, key):        if not isinstance(key, tuple):            self.locator = self.selector_to_locator(key)        else:            self.locator = key        if isinstance(self.locator, tuple):            try:                yIEld            except:                print(f"定位失败:定位方式->{self.locator[0]}, value值->{self.locator[1]}")                return False    def find(self, key, timeout=None, value=None):        """        页面查找单个元素        :param key: 元素的元组(By, value)        :param timeout: 超时时间        :param value: 需要查找的值        """        with self.find_base(key):            if timeout is None:                timeout = SettingBase.UI_WAIT_TIME            ele = self.__find_value(self.locator, timeout, value)            return ele    def not_find(self, key, timeout=None, value=None):        """        页面查找没有此元素        :param key: 元素的元组(By, value)        :param timeout: 超时时间        :param value:需要查找的值        """        with self.find_base(key):            if timeout is None:                timeout = SettingBase.UI_WAIT_TIME            self.__not_find_value(self.locator, timeout, value)    def __find_value(self, locator, timeout, value):        if value is None:            # presence_of_element_located 不关心元素是否可见,只要元素存在在页面中即可            ele = webdriverwait(self.driver, timeout, SettingBase.PolL_FREQUENCY).until(                 ES.presence_of_element_located(locator))        else:            # 判断元素中是否存在指定的文本,返回布尔值            ele = webdriverwait(self.driver, timeout, SettingBase.PolL_FREQUENCY).until(                ES.text_to_be_present_in_element(locator, value))        return ele    def __not_find_value(self, locator, timeout, value):        if value is None:            # presence_of_element_located 不关心元素是否可见,只要元素存在在页面中即可            bools = webdriverwait(self.driver, timeout, SettingBase.PolL_FREQUENCY).until_not(                ES.presence_of_element_located(locator))        else:            # 判断元素中是否存在指定的文本,返回布尔值            bools = webdriverwait(self.driver, timeout, SettingBase.PolL_FREQUENCY).until_not(                ES.text_to_be_present_in_element(locator, value))        return bools    def finds(self, key, timeout=None):        """        页面查找多个元素        :param key: 元素的元组(By, value)        :param timeout: 超时时间        :return: List or False        """        with self.find_base(key):            if timeout is None:                timeout = SettingBase.UI_WAIT_TIME            # presence_of_all_elements_located 等待所有locator元素都加载出来,返回List            eles = webdriverwait(self.driver, timeout, SettingBase.PolL_FREQUENCY).until(                ES.presence_of_all_elements_located(self.locator))            return eles    def find_Title(self, key, timeout=None):        """        Title中包含元素        :param key: 元素的元组(By, value)        :param timeout: 超时时间        :return: True when the Title matches, False otherwise        """        with self.find_base(key):            if timeout is None:                timeout = SettingBase.UI_WAIT_TIME            # 判断元素中是否存在包含Title,返回布尔值            ele = webdriverwait(self.driver, timeout, SettingBase.PolL_FREQUENCY).until(                ES.Title_contains(self.locator))            return ele

 

总结

以上是内存溢出为你收集整理的Python 封装selenium元素定位FindElement类全部内容,希望文章能够帮你解决Python 封装selenium元素定位FindElement类所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/langs/1189008.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-06-03
下一篇 2022-06-03

发表评论

登录后才能评论

评论列表(0条)

保存