从yaml文件读取内容使其转换为可识别的WebElement类型的值,进行操作
作者:互联网
# FindBy.py文件 作用是将yaml格式中的Type的值转换为可被By对象可识别的元组,被FindWebElement调用 # -*- coding:utf-8 -*- # explain :元素定位方式说明,用于返回By类型 import sys from selenium.common.exceptions import NoSuchElementException from selenium.webdriver.common.by import By from com.chanpin.initialize.Start import Primary class FindBy: def __init__(self, Type): """ 用于返回loaction元组的by类型 :param Type: yaml文件的Type值 """ self.__Type = Type # 定义查找元素 def find_by(self) -> By: """ 通过'id', 'name', 'class', 'tag', 'link', 'plink', 'css', 'xpath'查找相应的locationby :rtype locationby : selenium.webdriver.common.by.By :return locationby 对应的location元组的By的值 """ """定位元素""" by = self.__Type locationby = None if by in ['id', 'name', 'class', 'tag', 'link', 'plink', 'css', 'xpath']: try: if by == 'id': locationby = By.ID elif by == 'name': locationby = By.NAME elif by == 'class': locationby = By.CLASS_NAME elif by == 'tag': locationby = By.TAG_NAME elif by == 'link': locationby = By.LINK_TEXT elif by == 'plink': locationby = By.PARTIAL_LINK_TEXT elif by == 'css': locationby = By.CSS_SELECTOR elif by == 'xpath': locationby = By.XPATH else: Primary.Log.error("没有找到对应元素Type类型,请使用'id', 'name', 'class', 'tag', 'link', 'plink', 'css', 'xpath'") # Log.info('元素定位成功。定位方式:%s,使用的值%s:' % (by, value)) return locationby except NoSuchElementException as e: # print("no suceelement") method = sys._getframe(1).f_code.co_name # 调用该函数的函数名字,如果没有被调用,则返回<module> path = sys._getframe(1).f_code.co_filename # 调用该函数的文件名字,如果没有被调用,则返回<module> Primary.Log.error("在执行【" + path + "】文件中的【" + method + "】方法时出现错误" + "没有找到对应的元素" + e.__str__()) else: # print("输入的元素定位方式错误") Primary.Log.error("输入的元素定位方式错误,请使用'id', 'name', 'class', 'tag', 'link', 'plink', 'css', 'xpath'") # --FindWebElemnet.py文件,该文件的作用是将yaml文件中Type值作为元素定位的方式值,Value作为元素内容值,两个方法分别返回WebElement对象和由WebElement对象组成的list列表,被ReadyamlToElement类调用 # -*- coding:utf-8 -*- # explain : WebElement元素定位,给GetelementFromYaml使用 from com.chanpin.initialize.Start import Primary from selenium.common.exceptions import NoSuchElementException from selenium.webdriver.remote import webelement class FindWebElement: def __init__(self, driver, Type, Value): """ :type driver: selenium.webdriver.remote.webdriver.WebDriver 实例化findWebElement :param driver: :param Type: yaml文件的Type值 :param Value: yaml文件的value值 """ self.__driver = driver self.__Type = Type self.__Value = Value # 定义查找元素 def find_element(self, yamlpath, key) -> webelement: """ 通过'id', 'name', 'class', 'tag', 'link', 'plink', 'css', 'xpath'查找相应的元素 :return: WebElemt对应 """ """定位元素""" by = self.__Type value = self.__Value element = None if by in ['id', 'name', 'class', 'tag', 'link', 'plink', 'css', 'xpath']: # noinspection PyBroadException try: if by == 'id': element = self.__driver.find_element_by_id(value) elif by == 'name': element = self.__driver.find_element_by_name(value) elif by == 'class': element = self.__driver.find_element_by_class_name(value) elif by == 'tag': element = self.__driver.find_element_by_tag_name(value) elif by == 'link': element = self.__driver.find_element_by_link_text(value) elif by == 'plink': element = self.__driver.find_element_by_partial_link_text(value) elif by == 'css': element = self.__driver.find_element_by_css_selector(value) elif by == 'xpath': element = self.__driver.find_element_by_xpath(value) else: Primary.Log.error("没有找到对应元素Type类型,请使用'id', 'name', " "'class', 'tag', 'link', 'plink', 'css', 'xpath'") # Log.info('元素定位成功。定位方式:%s,使用的值%s:' % (by, value)) return element except NoSuchElementException as e: # print("no suceelement") Primary.Log.error("yaml文件为:" + yamlpath + ";页面中没有定位key值为'" + key + "'对应的元素;" + e.__str__() + ";请确保定位准确。") # self.get_img() # 调用截图 else: # print("输入的元素定位方式错误") Primary.Log.error("输入的元素定位方式错误,请使用'id', 'name', 'class', " "'tag', 'link', 'plink', 'css', 'xpath'") def find_elements(self, yamlpath, key) -> list: """ 通过'id', 'name', 'class', 'tag', 'link', 'plink', 'css', 'xpath'查找相应的元素 :return: 当list_elements只有1个时直接返回selenium.webdriver.remote.webelement类,否则返回list :rtype list_elements : list of WebElement """ """定位元素""" by = self.__Type value = self.__Value list_elements = None if by in ['id', 'name', 'class', 'tag', 'link', 'plink', 'css', 'xpath']: # noinspection PyBroadException try: if by == 'id': list_elements = self.__driver.find_elements_by_id(value) elif by == 'name': list_elements = self.__driver.find_elements_by_name(value) elif by == 'class': list_elements = self.__driver.find_elements_by_class_name(value) elif by == 'tag': list_elements = self.__driver.find_elements_by_tag_name(value) elif by == 'link': list_elements = self.__driver.find_elements_by_link_text(value) elif by == 'plink': list_elements = self.__driver.find_elements_by_partial_link_text(value) elif by == 'css': list_elements = self.__driver.find_elements_by_css_selector(value) elif by == 'xpath': list_elements = self.__driver.find_elements_by_xpath(value) else: Primary.Log.error("没有找到对应元素Type类型,请使用'id', 'name', 'class', " "'tag', 'link', 'plink', 'css', 'xpath'") # Log.info('元素定位成功。定位方式:%s,使用的值%s:' % (by, value)) return list_elements except NoSuchElementException as e: # print("no suceelement") Primary.Log.error("yaml文件为:" + yamlpath + ";页面中没有定位key值为'" + key + "'对应的元素;" + e.__str__() + ";请确保定位准确。") # self.get_img() # 调用截图 else: # print("输入的元素定位方式错误") Primary.Log.error("输入的元素定位方式错误,请使用'id', 'name', 'class', " "'tag', 'link', 'plink', 'css', 'xpath'") # GetelementFromYaml.py文件,真正被调用其他模块的调用的类,通过传入yaml文件中的key值根据情况获得真正能使用的WebElement对象,进行元素点击输入等操作 # -*- coding:utf-8 -*- # explain : 从yaml文件中读取返回webelement import yaml from selenium.webdriver.remote.webdriver import WebDriver from selenium.webdriver.support.wait import WebDriverWait from selenium.webdriver.remote.webelement import WebElement from selenium.webdriver.support import expected_conditions as EC from selenium.common.exceptions import TimeoutException from com.chanpin.utils.FindWebElemet import FindWebElement from com.chanpin.utils.FindBy import FindBy from com.chanpin.initialize.Start import Primary # yaml文件位置 ''' 初始化添加对应的yaml文件位置,然后调用getAllValue方法,调用 getelement方法获取到对应的webelement 改进方法:保证yaml的文件名和test_case的文件名或者类名一致 ''' class ReadyamlToElement: """ 通过yaml文件获取相应的元素定位,方法如下: getelement(self, key) : 返回WebElement类元素 getelements(self, key) : 返回list getelementsintnum(self, key, intnum=0) : 返回WebElement类元素 getelementsstringtext(self, key, strtext: str, attributename=None) : 返回WebElement类元素 waitgetelement(self, key) : 返回WebElement类元素 waitgetelements(self, key) : 返回list waitgetelement_clickable(self, key) : 返回WebElement类元素 getlocator(self, key) : 返回tuple """ def __init__(self, driver: WebDriver, yamlpath): """ 实例化readyaml类 :param driver: :param yamlpath: 对应的yaml文件名 """ self.__yamlPath = yamlpath self.__driver = driver def __getAllValue(self): """ 将指定yaml文件转化为dic字典,key为 :return dic字典 """ ''' curPath = os.path.dirname(os.path.realpath(__file__)) # 获取yaml文件路径 yamlPath = os.path.join(curPath, "cfgyaml.yaml") print(type(yamlPath)) ''' with open(self.__yamlPath, 'r', encoding='utf-8') as f: cfg = f.read() dic_yaml = yaml.load(cfg, Loader=yaml.FullLoader) # 用load方法转字典 return dic_yaml ''' 通过key获取到对应的webelement,改进方法,将其全部遍历后,生成key:element的字典,从里面取对应的数据 ''' def getelement(self, key) -> WebElement: """ 通过yaml文件的key获取到对应的Webelement :param key: yaml文件的key :return: 对应的WebElement对象 :rtype keyelement: selenium.webdriver.remote.webelement """ dic = ReadyamlToElement(self.__driver, self.__yamlPath).__getAllValue() # 接受readyaml().getAllValue()返回的dict if key in dic: list_value = dic.get(key) # list_Type_value = list_value[0].split(":")[1] #获取type的值 # list_Value_value = list_value[1].split(":")[1] #获取value的值 list_Type_value = list_value.get("Type") # 获取type的值 list_Value_value = list_value.get("Value") # 获取value的值 keyelement = FindWebElement(self.__driver, list_Type_value, list_Value_value).find_element(self.__yamlPath, key) # 用于返回webelement对象 Primary.Log.info(self.__yamlPath + "中获取到该element,通过该key:" + key) return keyelement else: Primary.Log.error(self.__yamlPath + "中不存在该key:" + key + ":关闭程序") # self.__driver.quit() def getelements(self, key): """ 通过yaml文件的key获取到对应的Webelement :param key: yaml文件的key :return: 对应的WebElement对象 类型为WebElement :rtype keyelements: list """ dic = ReadyamlToElement(self.__driver, self.__yamlPath).__getAllValue() # 接受readyaml().getAllValue()返回的dict # print(dic) if key in dic: list_value = dic.get(key) list_Type_value = list_value.get("Type") # 获取type的值 list_Value_value = list_value.get("Value") # 获取value的值 keyelementlist = FindWebElement(self.__driver, list_Type_value, list_Value_value).find_elements(self.__yamlPath, key) # 用于返回webelement对象 Primary.Log.info(self.__yamlPath + "中获取到该element,通过该key:" + key) return keyelementlist else: Primary.Log.error(self.__yamlPath + "中不存在该key:" + key + ":关闭程序") # self.__driver.quit() def getelementsintnum(self, key, intnum=0) -> WebElement: """ 通过yaml文件的key获取一组WebElemnts,通过数组下标获取对应的WebElemnt :param key: yaml文件的key :param intnum 数组下标 :type key : String :type intnum : int :return: 对应的WebElement对象 :rtype keyelements[intnum]: selenium.webdriver.remote.webelement """ dic = ReadyamlToElement(self.__driver, self.__yamlPath).__getAllValue() # 接受readyaml().getAllValue()返回的dict # print(dic) list_keyelements = [] if key in dic: list_value = dic.get(key) # list_Type_value = list_value[0].split(":")[1] #获取type的值 # list_Value_value = list_value[1].split(":")[1] #获取value的值 list_Type_value = list_value.get("Type") # 获取type的值 list_Value_value = list_value.get("Value") # 获取value的值 list_keyelements = FindWebElement(self.__driver, list_Type_value, list_Value_value).find_elements(self.__yamlPath, key) # 用于返回webelement对象 Primary.Log.info(self.__yamlPath + "中获取到该element,通过该key:" + key) else: Primary.Log.error(self.__yamlPath + "中不存在该key:" + key + ":关闭程序") # self.__driver.quit() # 判断输入的值是否正确,正确则正确找到,否则就报空 if -len(list_keyelements) <= intnum < len(list_keyelements): return list_keyelements[intnum] else: Primary.Log.error("当前输入的intnum大于" + str(len(list_keyelements)) + "有问题") return None def getelementsstringtext(self, key, strtext: str, attributename=None) -> WebElement: """ 通过yaml文件的key获取一组WebElemnts,通过指定元素的值获取到对应的WebElemnt :param key: yaml文件的key :param strtext 对应元素的值 :type key : str :type strtext : str :param attributename 元素的属性值,Name of the attribute/property to retrieve :type attributename : str :return: 对应的WebElement对象 :rtype element: selenium.webdriver.remote.webelement """ dic = ReadyamlToElement(self.__driver, self.__yamlPath).__getAllValue() # 接受readyaml().getAllValue()返回的dict # print(dic) list_keyelements = [] if key in dic: list_value = dic.get(key) # list_Type_value = list_value[0].split(":")[1] #获取type的值 # list_Value_value = list_value[1].split(":")[1] #获取value的值 list_Type_value = list_value.get("Type") # 获取type的值 list_Value_value = list_value.get("Value") # 获取value的值 list_keyelements = FindWebElement(self.__driver, list_Type_value, list_Value_value).find_elements(self.__yamlPath, key) # 用于返回webelement对象 Primary.Log.info(self.__yamlPath + "中获取到该element,通过该key:" + key) else: Primary.Log.error(self.__yamlPath + "中不存在该key:" + key + ":关闭程序") # self.__driver.quit() # 判断输入的值是否正确,正确则正确找到,否则就报空 booleanfind = False element = None # 当没有设备属性值时用元素的text值 if attributename is None: for webelement in list_keyelements: if webelement.text == strtext: element = webelement booleanfind = True break # 当设备属性值时用元素的指定的属性值的内容 else: for webelement in list_keyelements: if webelement.get_attribute(attributename) == strtext: element = webelement booleanfind = True break if booleanfind: return element else: Primary.Log.error(self.__yamlPath + "中未找到对应的元素值,请确认" + strtext + "的正确") return element def waitgetelement(self, key) -> WebElement: """ 通过yaml文件的key获取到对应的Webelement,加入了显示等待,WeblEment.is_displayed()为真时返回该该webElement :param key: yaml文件的key :return: 对应的WebElement对象 """ try: inttime = Primary.readini.getvalue("webdriver", "implicit_waitTime") WebDriverWait(self, int(inttime)).until(lambda x: x.getelement(key).is_displayed()) return self.getelement(key) except Exception as e: Primary.Log.error(self.__yamlPath + "中该元素不存在:" + key + "导致错误:" + e.__str__()) return None # self.__driver.quit() def waitgetelements(self, key) -> list: """ 通过yaml文件的key获取到对应的Webelement,加入了显示等待,WeblEment.is_displayed()为真时返回该该webElement :param key: yaml文件的key :return: 对应的WebElement对象 """ try: inttime = Primary.readini.getvalue("webdriver", "implicit_waitTime") WebDriverWait(self, int(inttime)).until(lambda x: x.getelement(key).is_displayed()) return self.getelements(key) except Exception as e: Primary.Log.error(self.__yamlPath + "中该元素不存在:" + key + "导致错误:" + e.__str__()) return None # self.__driver.quit() def waitgetelement_clickable(self, key) -> WebElement: """ 通过yaml文件的key获取到对应的Webelement,当元素可以点击时返回webelement :param key: yaml文件的key :return: 对应的WebElement对象 :rtype keyelement: selenium.webdriver.remote.webelement """ dic = ReadyamlToElement(self.__driver, self.__yamlPath).__getAllValue() # 接受readyaml().getAllValue()返回的dict if key in dic: list_value = dic.get(key) list_Type_value = list_value.get("Type") # 获取type的值 list_Value_value = list_value.get("Value") # 获取value的值 try: # 显式等待时间 inttime = Primary.readini.getvalue("webdriver", "implicit_waitTime") # 配合expected_conditions当元素可以点击时返回element elemnet = WebDriverWait(self.__driver, int(inttime)).until(EC.element_to_be_clickable(( FindBy(list_Type_value).find_by(), list_Value_value)), (self.__yamlPath + "中No such element : 请检查该‘" + key + "’对应下的Value值是否能在页面上定位成功")) return elemnet except TimeoutException as e: Primary.Log.error(e.__str__()) return None def getlocator(self, key) -> tuple: """ 返回location 元组类型的值,主要给EC模块入参使用 :param key : yaml文件的key :return: tup_locator 元组类型的值如(By.Id,"value") :rtype tup_locator : tuple """ dic = ReadyamlToElement(self.__driver, self.__yamlPath).__getAllValue() # 接受readyaml().getAllValue()返回的dict if key in dic: list_value = dic.get(key) # list_Type_value = list_value[0].split(":")[1] #获取type的值 # list_Value_value = list_value[1].split(":")[1] #获取value的值 list_Type_value = list_value.get("Type") # 获取type的值 list_Value_value = list_value.get("Value") # 获取value的值 tup_locator = (FindBy(list_Type_value).find_by(), list_Value_value) if len(tup_locator) is None: Primary.Log.error(self.__yamlPath + "中请检查该‘" + key + "’的值是否正确") return None else: return tup_locator yaml文件格式样例 # -*- coding:utf-8 -*- # explain : 登录页面的元素定位 #Type : id, name, class, tag, link, plink, css, xpath #登录用户名输入框 login_username : # ReadyamlToElement类对象方法的实参 Type : css # 八大定位方式 Value : i.iconfont.icon-login+span+label>input # 定位值
说明:需要三个主要方法来实现WebElement对象的值
标签:.__,读取,self,list,value,WebElement,yaml,key,Type 来源: https://blog.51cto.com/11463754/2482578