# coding:utf-8
from seleniumimport webdriver
from selenium.webdriver.support.waitimport WebDriverWait
from selenium.webdriver.supportimport expected_conditionsas EC
from selenium.webdriver.common.byimport By
from selenium.webdriver.common.action_chainsimport ActionChains
from selenium.webdriver.support.selectimport Select
# By的用法
# driver.find_element("id", "kw")
# driver.find_element(By.ID, "kw")
class Base():
'''基于原生的selenium做二次封装'''
def __init__(self, driver):
self.driver = driver
self.timeout =10
self.t =0.5
def findElement(self, locator, value=''):
'''定位元素,返回元素对象,10s钟没定位到,Timeout异常,locator传元组'''
if not isinstance(locator, tuple):
print("locator的参数类型错误,必须穿元组类型,如:('id', 'kw')")
else:
print("正在定位元素,定位方式为→{0},定位元素为→{1}".format(locator[0], locator[1]))
if value !='':# value 值定位
ele = WebDriverWait(self.driver, self.timeout, self.t).until(
EC.text_to_be_present_in_element_value(locator, value))
return ele
else:# 默认为此方法常规定位
try:
ele = WebDriverWait(self.driver, self.timeout, self.t).until(
EC.presence_of_element_located(locator))
return ele
except:
print("定位失败,定位方式为→{0},定位元素为→{1}".format(locator[0], locator[1]))
return []
def findElements(self, locator, value=''):
'''定位元素,返回元素对象,10s钟没定位到,Timeout异常,locator传元组'''
if not isinstance(locator, tuple):
print("locator的参数类型错误,必须穿元组类型,如:('id', 'kw')")
else:
print("正在定位元素,定位方式为→{0},定位元素为→{1}".format(locator[0], locator[1]))
if value !='':# value 值定位
eles = WebDriverWait(self.driver, self.timeout, self.t).until(
EC.text_to_be_present_in_element_value(locator, value))
return eles
else:# 默认为此方法常规定位
try:
eles = WebDriverWait(self.driver, self.timeout, self.t).until(
EC.presence_of_all_elements_located(locator))
return eles
except:
print("定位失败,定位方式为→{0},定位元素为→{1}".format(locator[0], locator[1]))
return []
def sendKeys(self, locator, text=''):
try:
self.findElement(locator).send_keys(text)
except:
print("text:%s输入错误" % text)
def click(self, locator):
try:
self.findElement(locator).click()
except:
print("点击失败")
def clear(self, locator):
try:
self.findElement(locator).clear()
except:
print("清空内容失败")
def isSelected(self, locator, Type=''):
''' 判断元素是否被选中,返回bool值 及点(选中/取消选中)'''
ele =self.find(locator)
try:
if Type =='':# 如果type参数为空,返回元素是否为选中状态,True/False (默认)
r = ele.is_selected()
return r
elif Type =='click':# 如果type参数为click,执行元素的点击操作
ele.click()
else:
print("type参数 {0} 错误,仅可为click或".format(Type))
except:
return False
def isElementExist(self, locator):
'''判断单个元素是否在DOM(元素树)里面'''
try:
self.findElement(locator)
return True
except:
return False
def is_title(self, title=''):
'''判断当前页面title是否为title,返回bool值'''
try:
result = WebDriverWait(self.driver, self.timeout, self.t).until(EC.title_is(title))
return result
except:
return False
def is_title_contains(self, title=''):
'''判断当前页面title名是否含有title,返回bool值'''
try:
result = WebDriverWait(self.driver, self.timeout, self.t).until(EC.title_contains(title))
return result
except:
return False
def is_text_in_element(self, locator, text=''):
'''判断当前获取到的text含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 is_value_in_element(self, locator, _value=''):
'''返回bool值, value为空字符串,返回False'''
if not isinstance(locator, tuple):
print('locator参数类型错误,必须传元祖类型:loc = ("id", "value1")')
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 get_title(self):
'''获取title'''
return self.driver.title
def get_text(self, locator):
'''获取文本'''
try:
result =self.findElement(locator).text
return result
except:
print("获取text失败")
return ""
def get_attribute(self, locator, name):
'''获取属性'''
try:
element =self.findElement(locator)
return element.get_attribute(name)
except:
print("获取%s属性失败" % name)
return ""
def select_by_index(self, locator, index=0):
'''通过索引,index是索引第几个,从0开始,默认选第一个'''
element =self.find(locator)# 定位select这一栏
Select(element).select_by_index(index)
def select_by_value(self, locator, value):
'''通过value属性'''
element =self.find(locator)
Select(element).select_by_value(value)
def select_by_text(self, locator, text):
'''通过文本值定位'''
element =self.find(locator)
Select(element).select_by_visible_text(text)
# 这里只写了几种方法,后续有需要用到的定位方法或者操作,可以继续封装起来然后调用
if __name__ =='__main__':
driver = webdriver.Chrome()
web = Base(driver)
driver.get("https://account.cnblogs.com/signin")
# 举个列子,后续在case里面直接调用封装的就可以了
loc_1 = (By.CLASS_NAME, "login-title")
t = web.get_text(loc_1)
print(t)
网友评论