from selenium import webdriver
from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.support.select import Select
from selenium.webdriver.common.action_chains import ActionChains
from selenium.common.exceptions import ElementNotVisibleException
from selenium.common.exceptions import TimeoutException
'''封装selenium基本操作'''
class LocatorTypeError(Exception):
pass
class ElementNotFound(Exception):
pass
class Base():
'''基于原声的 selenium做二次封装'''
def __init__(self,driver:webdriver.Chrome,timeout=20,t=0.5):
self.driver = driver
self.timeout = timeout
self.t = t
def find(self,locator):
'''定位到元素,返回元素对象,没有定位到,Timeout异常'''
if not isinstance(locator,tuple):
raise LocatorTypeError('参数类型错误,locator必须是元祖类型 loc=("id","value1")')
else:
print("正在定位元素信息:定位方式->%s,value值->%s" %(locator[0],locator[1]))
try:
# ele=WebDriverWait(self.driver,self.timeout,self.t).until(EC.presence_of_element_located(locator))
ele = WebDriverWait(self.driver, self.timeout, self.t).until(EC.presence_of_element_located(locator))
except TimeoutException as msg:
raise ElementNotFound('定位元素出现超时!!!')
return ele
def finds(self,locator):
'''复数定位,返回elements对象'''
if not isinstance(locator,tuple):
raise LocatorTypeError('参数类型错误,locator必须是元祖')
else:
print("正在定位元素信息")
eles=WebDriverWait(self.driver,self.timeout,self.t)
return eles