class Base():
def __init__(self,driver):
self.driver = driver
self.timeout = 10
self.t = 0.5
#查找元素的另一种方法:
def findElementNew(self, locator):
#定位到元素,返回元素对象,没有定位到返回timeout异常
ele = WebDriverWait(self.driver,self.timeout,self.t).until(EC.presence_of_element_located(locator)) # *locator取变量locator的值,locator的值的类型是元祖
return ele
#封装定位方法,加等待,用10s的时间去查找元素,间隔查找时间05s
def findElenment(self, locator): #locator变量是定位器,给的值比如id和value,等同于:查找元素通过id的值value findElement(By.ID, "tests")
ele = WebDriverWait(self.driver,self.timeout,self.t).until(lambda x: x.find_element(*locator)) # *locator取变量locator的值,locator的值的类型是元祖
return ele
def findElements(self, locator): #locator变量是定位器,给的值比如id和value,等同于:查找元素通过id的值value findElement(By.ID, "tests")
try:
eles = WebDriverWait(self.driver,self.timeout,self.t).until(lambda x: x.find_elements(*locator)) # *locator取变量locator的值,locator的值的类型是元祖
return eles
except:
return []
#封装sendKeys方法
def sendKeys(self,locator, text):
ele = self.findElenment(locator) #通过locator(例如id和value)定位到元素
ele.send_keys(text) #传值
#封装click方法
def click(self, locator):
ele = self.findElenment(locator) #通过locator(例如id和value)定位到元素
ele.click() #点击
def click_by_index(self, locator, index):
ele = self.findElements(locator)[index]
ele.click()
def clear(self, locator): #清除方法
ele = self.findElenment(locator)
ele.clear()
def isSelected(self, locator): #判断元素被选中
ele = self.findElenment(locator)
sele = ele.is_selected()
return sele
def isElementExist(self, locator): #判断元素是否存在
try:
ele = self.findElenment(locator)
return True
except:
return False
def isElementExist2(self, locator): #判断元素是否存在
eles = self.findElements(locator)
n = len(eles)
if n == 0:
return False
elif n == 1:
return True
else:
print("定位到 %s 个元素" %n)
return True
def isTitle(self, _title):
'''返回bool值'''
try:
result = WebDriverWait(self.driver,self.timeout,self.t).until(EC.title_is(_title))
return result
except:
# print("实际title:", self.driver.title)
return False
def isTitleContains(self, _title):
'''返回bool值'''
result = WebDriverWait(self.driver,self.timeout,self.t).until(EC.title_contains(_title))
return result
def textToBePresentInElement(self, locator, _text):
'''返回bool值'''
try:
result = WebDriverWait(self.driver,self.timeout,self.t).until(EC.text_to_be_present_in_element(locator, _text))
return result
except:
return False
def textToBePresentInElementValue(self, locator, _value):
try:
result = WebDriverWait(self.driver,self.timeout,self.t).until(EC.text_to_be_present_in_element_value(locator, _value))
return result
except:
return False
def alertIsPresent(self): #存在alert返回元素对象
try:
result = WebDriverWait(self.driver,self.timeout,self.t).until(EC.alert_is_present())
return result
except:
return False
def getText(self, locator):
try:
text = self.findElenment(locator).text
return text
except:
return ""
def move_mouse_to(self, locator):
'''封装鼠标悬停方法'''
ele = self.findElenment(locator)
ActionChains(self.driver).move_to_element(ele).perform()
def select_by_index(self, locator, index=0):
ele = self.findElenment(locator)
Select(ele).select_by_index(index)
def select_by_value(self, locator, value):
ele = self.findElenment(locator)
Select(ele).select_by_value(value)
def select_by_text(self, locator, text):
ele = self.findElenment(locator)
Select(ele).select_by_visible_text(text)
def js_scroll_end(self):
'''滚动到底部'''
js_heig = "window.scrollTo(0, document.body.scrollHeight)"
self.driver.execute_script(js_heig)
def js_scroll_top(self):
'''滚动到顶部'''
js = "window.scrollTo(0, 0)"
self.driver.execute_script(js)
def js_focus(self, locator):
'''聚焦元素'''
target = self.findElenment(locator)
self.driver.execute_script("arguments[0].scrollIntoView();", target)