# coding:utf-8
from selenium import webdriver
from selenium.common.exceptions import NoSuchElementException
class BrowserEngine(object):
def __init__(self, driver = None):
self.driver = driver
"""控制启动浏览器"""
def get_browser(self, browser = 'Chrome'):
"""启动判断浏览器"""
if browser == 'Firefox':
self.driver = webdriver.Firefox()
elif browser == 'Chrome':
self.driver = webdriver.Chrome()
elif browser == 'IE':
self.driver = webdriver.Ie()
self.driver.maximize_window()
self.driver.implicitly_wait(30)
self.driver.get("https://sso.dev.weixiuhui.cn/cas/login")
return self.driver
def findElement(self, controlInfo):
element = ""
if (controlInfo.startswith("//")):
element = self.driver.find_element_by_xpath(controlInfo)
elif (":id/" in controlInfo or ":string/" in controlInfo):
element = self.driver.find_element_by_id(controlInfo)
else:
# 剩下的字符串没有特点,无法区分,因此先尝试通过名称查找
try:
element = self.driver.find_element_by_name(controlInfo)
except:
# 如果通过名称不能找到则通过class name查找
element = self.driver.find_element_by_class_name(controlInfo)
return element
def findElements(self, controlInfo):
elements = ""
if (controlInfo.startswith("//")):
elements = self.driver.find_elements_by_xpath(controlInfo)
elif (":id/" in controlInfo):
elements = self.driver.find_elements_by_id(controlInfo)
else:
elements = self.driver.find_elements_by_name(controlInfo)
if (len(elements) == 0):
elements = self.driver.find_elements_by_class_name(controlInfo)
return elements
"""点击操作"""
def clickElement(self, elementInfo):
element = self.findElement(elementInfo)
element.click()
"""输入文本"""
def writeElement(self,elementInfo,value):
element = self.findElement(elementInfo)
element.send_keys(value)
"""清除文本"""
def clearTextEdit(self, elementInfo):
element = self.findElement(elementInfo)
element.clear()
# 断言方法
def verification(self, Info, data):
# 判断是是否相等
# 用法 self.verification("元素",u"对比项")
try:
self.assertEqual(self.findElement(Info).text, data)
except NoSuchElementException as msg:
print (u"查找元素异常%s" % msg)
使用这个断言方法报对象没有属性,这个是什么原因导致的