真实的国产乱ⅩXXX66竹夫人,五月香六月婷婷激情综合,亚洲日本VA一区二区三区,亚洲精品一区二区三区麻豆

成都創(chuàng)新互聯(lián)網(wǎng)站制作重慶分公司

定位元素操作

#?-*-?coding:utf-8?-*-
#?date?=?2019/5/13
#?name?=?"yedeng"
#?project_name?=?python
#?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?utils.FindWebElemet?import?FindWebElement
from?utils.FindBy?import?FindBy
from?utils.log?import?Log
from?utils.ReadIni?import?Readini

#?yaml文件位置
#?Readin?讀取配置文件中的等待時(shí)間
'''
???初始化添加對(duì)應(yīng)的yaml文件位置,然后調(diào)用getAllValue方法,調(diào)用?getelement方法獲取到對(duì)應(yīng)的webelement
???改進(jìn)方法:保證yaml的文件名和test_case的文件名或者類名一致
'''


class?ReadyamlToElement:
????#?獲取當(dāng)前腳本所在文件夾路徑

????def?__init__(self,?driver:?WebDriver,?yamlpath):
????????"""
????????實(shí)例化readyaml類
????????:param?driver:
????????:param?yamlpath:?對(duì)應(yīng)的yaml文件名
????????"""
????????self.__yamlPath?=?yamlpath
????????self.__driver?=?driver

????def?__getAllValue(self):
????????"""
???????????將指定yaml文件轉(zhuǎn)化為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方法轉(zhuǎn)字典
????????return?dic_yaml

????'''
???????通過key獲取到對(duì)應(yīng)的webelement,改進(jìn)方法,將其全部遍歷后,生成key:element的字典,從里面取對(duì)應(yīng)的數(shù)據(jù)
????'''

????def?getelement(self,?key)?->?WebElement:
????????"""
????????通過yaml文件的key獲取到對(duì)應(yīng)的Webelement
????????:param?key:?yaml文件的key
????????:return:?對(duì)應(yīng)的WebElement對(duì)象
????????:rtype?keyelement:?selenium.webdriver.remote.webelement
????????"""
????????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[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對(duì)象
????????????Log.info(self.__yamlPath?+?"中獲取到該element,通過該key:"?+?key)
????????????return?keyelement
????????else:
????????????Log.error(self.__yamlPath?+?"中不存在該key:"?+?key?+?":關(guān)閉程序")
????????????#?self.__driver.quit()

????def?getelements(self,?key)?->?list:
????????"""
????????通過yaml文件的key獲取到對(duì)應(yīng)的Webelement
????????:param?key:?yaml文件的key
????????:return:?對(duì)應(yīng)的WebElement對(duì)象?類型為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[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的值
????????????keyelementlist?=?FindWebElement(self.__driver,?list_Type_value,
????????????????????????????????????????????list_Value_value).find_elements(self.__yamlPath,?key)??#?用于返回webelement對(duì)象
????????????Log.info(self.__yamlPath?+?"中獲取到該element,通過該key:"?+?key)
????????????return?keyelementlist
????????else:
????????????Log.error(self.__yamlPath?+?"中不存在該key:"?+?key?+?":關(guān)閉程序")
????????????#?self.__driver.quit()

????def?getelementsintnum(self,?key,?intnum=0)?->?WebElement:
????????"""
????????通過yaml文件的key獲取一組WebElemnts,通過數(shù)組下標(biāo)獲取對(duì)應(yīng)的WebElemnt
????????:param?key:?yaml文件的key
????????:param?intnum?數(shù)組下標(biāo)
????????:type?key?:?String
????????:type?intnum?:?int
????????:return:?對(duì)應(yīng)的WebElement對(duì)象
????????: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對(duì)象
????????????Log.info(self.__yamlPath?+?"中獲取到該element,通過該key:"?+?key)
????????else:
????????????Log.error(self.__yamlPath?+?"中不存在該key:"?+?key?+?":關(guān)閉程序")
????????????#?self.__driver.quit()
????????????#?判斷輸入的值是否正確,正確則正確找到,否則就報(bào)空
????????if?-len(list_keyelements)?<=?intnum??WebElement:
????????????"""
????????????通過yaml文件的key獲取一組WebElemnts,通過指定元素的值獲取到對(duì)應(yīng)的WebElemnt
????????????:param?key:?yaml文件的key
????????????:param?strtext?對(duì)應(yīng)元素的值
????????????:type?key?:?str
????????????:type?strtext?:?str
????????????:param?attributename?元素的屬性值,Name?of?the?attribute/property?to?retrieve
????????????:type?attributename?:?str
????????????:return:?對(duì)應(yīng)的WebElement對(duì)象
????????????: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對(duì)象
????????????????Log.info(self.__yamlPath?+?"中獲取到該element,通過該key:"?+?key)
????????????else:
????????????????Log.error(self.__yamlPath?+?"中不存在該key:"?+?key?+?":關(guān)閉程序")
????????????????#?self.__driver.quit()
????????????????#?判斷輸入的值是否正確,正確則正確找到,否則就報(bào)空
????????????booleanfind?=?False
????????????element?=?None
????????????#?當(dāng)沒有設(shè)備屬性值時(shí)用元素的text值
????????????if?attributename?is?None:
????????????????for?webelement?in?list_keyelements:
????????????????????if?webelement.text?==?strtext:
????????????????????????element?=?webelement
????????????????????????booleanfind?=?True
????????????????????????break
????????????#?當(dāng)設(shè)備屬性值時(shí)用元素的指定的屬性值的內(nèi)容
????????????else:
????????????????for?webelement?in?list_keyelements:
????????????????????if?webelement.get_attribute(attributename)?==?strtext:
????????????????????????element?=?webelement
????????????????????????booleanfind?=?True
????????????????????????break
????????
????????????if?booleanfind:
????????????????return?element
????????????else:
????????????????Log.error(self.__yamlPath?+?"中未找到對(duì)應(yīng)的元素值,請(qǐng)確認(rèn)"?+?strtext?+?"的正確")
????????????????return?element

????def?waitgetelement(self,?key):
????????"""
????????通過yaml文件的key獲取到對(duì)應(yīng)的Webelement,加入了顯示等待,WeblEment.is_displayed()為真時(shí)返回該該webElement
????????:param?key:??yaml文件的key
????????:return:?對(duì)應(yīng)的WebElement對(duì)象
????????"""
????????try:
????????????inttime?=?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:
????????????Log.error(self.__yamlPath?+?"中該元素不存在:"?+?key?+?"導(dǎo)致錯(cuò)誤:"?+?e.__str__())
????????????#?self.__driver.quit()

????def?waitgetelements(self,?key):
????????"""
????????通過yaml文件的key獲取到對(duì)應(yīng)的Webelement,加入了顯示等待,WeblEment.is_displayed()為真時(shí)返回該該webElement
????????:param?key:??yaml文件的key
????????:return:?對(duì)應(yīng)的WebElement對(duì)象
????????"""
????????try:
????????????inttime?=?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:
????????????Log.error(self.__yamlPath?+?"中該元素不存在:"?+?key?+?"導(dǎo)致錯(cuò)誤:"?+?e.__str__())
????????????#?self.__driver.quit()

????def?waitgetelement_clickable(self,?key)?->?WebElement:
????????"""
????????通過yaml文件的key獲取到對(duì)應(yīng)的Webelement,當(dāng)元素可以點(diǎn)擊時(shí)返回webelement
????????:param?key:?yaml文件的key
????????:return:?對(duì)應(yīng)的WebElement對(duì)象
????????: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:
????????????????#?顯式等待時(shí)間
????????????????inttime?=?Readini().getvalue("webdriver",?"implicit_waitTime")
????????????????#?配合expected_conditions當(dāng)元素可以點(diǎn)擊時(shí)返回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?:?請(qǐng)檢查該‘"?+?key?+
????????????????????????????????????????????????????????????????????????????"’對(duì)應(yīng)下的Value值是否能在頁面上定位成功"))
????????????????return?elemnet
????????????except?TimeoutException?as?e:
????????????????Log.error(e.__str__())

????def?getlocator(self,?key)?->?tuple:
????????"""
????????返回location?元組類型的值,主要給EC模塊入?yún)⑹褂?????????: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:
????????????????Log.error(self.__yamlPath?+?"中請(qǐng)檢查該‘"?+?key?+?"’的值是否正確")
????????????else:
????????????????return?tup_locator

名稱欄目:定位元素操作
路徑分享:http://weahome.cn/article/ihpegj.html

其他資訊

在線咨詢

微信咨詢

電話咨詢

028-86922220(工作日)

18980820575(7×24)

提交需求

返回頂部