# -*- 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)