封装元素是否显示、属性、执行方法

# -*- coding:utf-8 -*-

import time

from selenium.webdriver.support.ui import WebDriverWait

#from publicmethod import pcdriver

 

 

class ElementMethod():

def __init__(self,driver):

self.dr=driver

 

def iselementexist(self,element,attribute):

if attribute=='id':

findelement=self.dr.find_elements_by_id(element)

elif attribute=='xpath':

findelement=self.dr.find_elements_by_xpath(element)

elif attribute=='css':

findelement=self.dr.find_elements_by_css_selector(element)

elif attribute=='name':

findelement=self.dr.find_elements_by_name(element)

elif attribute == 'link_text':

findelement = self.dr.find_elements_by_link_text(element)

elif attribute == 'tag_name':

findelement = self.dr.find_elements_by_tag_name(element)

elif attribute == 'class_name':

findelement = self.dr.find_elements_by_class_name(element)

elif attribute=='android_uiautomator':

findelement=self.dr.find_elements_by_android_uiautomator(element)

if len(findelement) == 1 :

#print("找到%s个元素:%s" % (len(s),id))

return True

elif len(findelement) >= 1 :

#print("找到%s个元素1:%s" % (len(s), id) )

return True

else:

#print("元素未显示:%s" % element)

return False

def forelementexist(self,element,attribute,circlenum):

for i in range(1, circlenum):

time.sleep(0.2)

try:

if ElementMethod(self.dr).iselementexist(element,attribute):

if attribute == 'id':

return self.dr.find_element_by_id(element)

elif attribute == 'xpath':

return self.dr.find_element_by_xpath(element)

elif attribute == 'css':

return self.dr.find_element_by_css_selector(element)

elif attribute == 'name':

return self.dr.find_element_by_name(element)

elif attribute == 'link_text':

return self.dr.find_element_by_link_text(element)

elif attribute == 'tag_name':

return self.dr.find_element_by_tag_name(element)

elif attribute == 'class_name':

return self.dr.find_element_by_class_name(element)

elif attribute == 'android_uiautomator':

return self.dr.find_element_by_android_uiautomator(element)

except Exception as e:

print(e)

print("元素未显示:%s" % element)

def elemethod(self,element,attribute,method,key='',circlenum=500):

if ElementMethod(self.dr).forelementexist(element,attribute,circlenum)==None:

return True

elif method=='click':

ElementMethod(self.dr).forelementexist(element, attribute,circlenum).click()

return False

elif method=='sendkey':

ElementMethod(self.dr).forelementexist(element, attribute,circlenum).send_keys(key)

return False

elif method=='text':

return ElementMethod(self.dr).forelementexist(element, attribute,circlenum).text

elif method=='clear':

ElementMethod(self.dr).forelementexist(element, attribute,circlenum).clear()

return False

elif method=='attribute':

ElementMethod(self.dr).forelementexist(element, attribute,circlenum).get_attribute()

return False

def waitelement(self,element,waittime=10):

try:

WebDriverWait(self.dr,waittime).until(lambda dr:dr.find_element_by_xpath(element).is_displayed())

except Exception as e:

print(e)

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值