# -*- coding: utf-8 -*-
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.support.ui import Select
from selenium.webdriver.support.ui import WebDriverWait
from selenium.common.exceptions import NoSuchElementException
from selenium.common.exceptions import NoAlertPresentException
from selenium.webdriver.common.keys import Keys
from selenium.webdriver import ActionChains
import unittest, time, re
import const
'''
ID = "id"
XPATH = "xpath"
LINK_TEXT = "link text"
PARTIAL_LINK_TEXT = "partial link text"
NAME = "name"
TAG_NAME = "tag name"
CLASS_NAME = "class name"
CSS_SELEC = "css selector"
'''
class CoreWeb(unittest.TestCase):
"""docstring for CoreWeb."""
def __init__(self, driver):
super(CoreWeb, self).__init__()
self.driver = driver
def setUp(self,url):
self.base_url =url
self.verificationErrors = []
self.accept_next_alert = True
self.driver.get(self.base_url)
self.driver.implicitly_wait(15)
self.driver.maximize_window()
def tearDown(self):
self.driver.quit()
self.assertEqual([], self.verificationErrors)
def find_element(self,how,what):
element = WebDriverWait(self.driver,30).until(lambda x: x.find_element(by=how, value=what))
return element
def find_elements(self,how,what):
elements = WebDriverWait(self.driver,30).until(lambda x: x.find_elements(by=how, value=what))
return elements
def is_element_present(self, how, what):
try: self.find_element(how,what)
except NoSuchElementException as e: return False
return True
def is_alert_present(self):
try: self.driver.switch_to_alert()
except NoAlertPresentException as e: return False
return True
def close_alert_and_get_its_text(self):
try:
alert = self.driver.switch_to_alert()
alert_text = alert.text
if self.accept_next_alert:
alert.accept()
else:
alert.dismiss()
return alert_text
finally: self.accept_next_alert = True
# 终极方法
def operation(self,element,elementType,how,what,*related_value):
if elementType is "input":
self.clear(how,what)
self.send_keys(how,what,related_value[0])
elif elementType is "click":
self.click(how,what)
elif elementType == "frame":
self.driver.switch_to.frame(0)
elif elementType == "window":
self.switch_window(related_value)
elif elementType == "is_alert_present":
self.is_alert_present()
elif elementType == "is_element_present":
self.is_element_present(how, what)
elif elementType == "select_by_index":
self.select_by_index(how,what,related_value)
elif elementType == "select_by_value":
self.select_by_value(how,what,related_value)
elif elementType == "select_by_visible_text":
self.select_by_visible_text(how,what,related_value)
elif elementType is "setUp":
self.setUp(related_value[0])
elif elementType == "tearDown":
self.tearDown()
elif elementType == "find_element":
self.find_element(how,what)
# 扩充书中的48种使用方法
# 01.访问某个网站
def get(self,url):
self.driver.get(url)
# 02.网页的前进和后退
def forward(self):
self.driver.forward()
def back(self):
self.driver.back()
# 03.刷新当前网页
def refresh(self):
self.driver.refresh()
# 04.浏览器窗口最大化
def maximize_window(self):
self.driver.maximize_window()
# 05.获取并设置当前窗口的位置
def get_window_position(self):
return self.driver.get_window_position()
# 06.获取并设置当前窗口的大小
def get_window_rect(self):
return self.get_window_rect()
# 07.获取页面的Title属性值
def title(self):
return self.driver.title
# 08.获取页面HTML源代码
def page_source(self):
return self.driver.page_source
# 09.获取当前页面的URL地址
def current_url(self):
return self.driver.current_url
# 10. 用于切换window 窗口, 通过页面标题或者页面相关信息文
def switch_window(self,page_keywords):
success=False
for x in self.driver.window_handles:
self.driver.switch_to.window(x)
if page_keywords in self.driver.page_source.encode("utf-8"):
print(self.driver.title)
success=True
print(">>>Switch window is successful<<<")
break
else:
print("not find the windows....."+page_keywords[0])
return success
# 11.获取页面元素的基本信息
# 获取标签名
def tag_name(self,how, what):
return self.find_element(how, what).tag_name()
#获取元素大小
def size(self,how, what):
return self.find_element(how, what).size()
#获取元素的ID
def id(self, how, what):
return self.find_element(how, what).id()
#获取元素的位置信息
def location(self, how, what):
return self.find_element(how, what).location()
#获取屏幕左上角的位置
def location_once_scrolled_into_view(self, how, what):
return self.find_element(how, what).location_once_scrolled_into_view()
#获取当前元素的父元素
def parent(self, how, what):
return self.driver.find_element(how, what).parent()
#一个包含元素大小和位置的字典
def rect(self, how, what):
return self.driver.find_element(how, what).rect()
#获取当前元素的屏幕截图作为二进制数据。
def screenshot_as_png(self, how, what):
return self.driver.find_element(how, what).screenshot_as_png()
# 获取当前元素的屏幕截图,作为base64编码的字符串
def screenshot_as_base64(self, how, what):
return self.driver.find_element(how, what).screenshot_as_base64()
#获取当前元素的截图,给定名称
def screenshot(self, how, what, filename):
return self.driver.find_element(how, what).screenshot(filename)
#12.获取页面元素的文本信息
def text(self, how, what):
return self.driver.find_element(how, what).text()
# 13.判断页面元素是否可见
def is_displayed(self, how, what):
return self.driver.find_element(how, what).is_displayed()
# 14.判断页面元素是否可操作
def is_enabled(self, how, what):
return self.driver.find_element(how, what).is_enabled()
def is_selected(self,how, what):
self.find_element(how, what).is_selected()
# 15.获取页面元素的属性
def get_attribute(self, how, what, attribute_name):
self.find_element(how, what).get_attribute(attribute_name)
# 16.获取页面元素的CSS属性值
def value_of_css_property(self, how, what, property_name):
self.find_element(how, what).value_of_css_property(property_name)
# 17.清空输入框中的内容
def clear(self, how, what):
self.find_element(how, what).clear()
#18.在输入框中输入指定内容
def send_keys(self,how,what,inputContent):
self.find_element(how,what).send_keys(inputContent)
#19.单击按钮
def click(self,how,what):
self.find_element(how,what).click()
# 20.双击某个元素
def double_click(self,locator_type, locator_value):
action_chains = ActionChains(self.driver)
action_chains.double_click(self.find_element(locator_type, locator_value)).perform()
#21.操作单选下拉列表
def select_by_index(self,how, what,operate_value):
select = Select(self.find_element(how,what))
select.select_by_index(operate_value)
def select_by_visible_text(self,how, what, operate_value):
select = Select(self.find_element(how, what))
select.select_by_visible_text(operate_value)
def select_by_value(self,how, what, operate_value):
select = Select(self.find_element(how, what))
select.select_by_value(operate_value)
# 22.断言单选列表选项值
def select_options(self,how,what):
select_element = Select(self.find_element(how,what))
select_options = select_element.options
return select_options
# 23.操作多选的选择列表
# 24.操作可以输入的下拉列表
def seect_input(self,how,what,keyword):
self.find_element(how,what).send_keys(keyword,Keys.ARROW_DOWN)
self.find_element(how,what).send_keys(Keys.ARROW_DOWN)
self.find_element(how,what).send_keys(Keys.ENTER)
# 25.操作单选框
# 26.操作复选框
# 27.断言页面源码中的关键字
def assert_information(self,how,what,assertInfo):
assert assertInfo in self.driver.page_source, u"该页面不存在该关键字信息"
#28.对当前浏览器窗口截屏
def get_screenshot_as_file(self,filename):
result =self.driver.get_screenshot_as_file(filename)
#29.拖拽页面元素
#30.模拟键盘单个按键操作
def enter(self,how,what):
self.find_element(how,what).send_keys(Keys.ENTER)
def F12(self,how,what):
self.find_element(how,what).send_keys(Keys.F12)
def control(self,how,what):
self.find_element(how,what).send_keys(Keys.CONTROL)
def cancel(self,how,what):
self.find_element(how,what).send_keys(Keys.CANCEL)
#31.模拟组合按键操作
#32.模拟鼠标右键
#33.模拟鼠标左键按下与释放
# 34.保持鼠标悬停在某个元素上
# 35.判断页面元素是否存在
# 36.隐式等待
# 37.显式等待
# 38.显式等待中期望的场景
# 39.使用Title属性识别和操作新弹出的浏览器窗口
# 40.通过页面的关键内容识别和操作新浏览器窗口
# 41.操作Frame中的页面元素
# 42.使用Frame中的HTML源码内容操作Frame
# 43.操作IFrame中的页面元素
# 44.操作JavaScript的Alert弹窗
# 45.操作JavaScript的confirm弹窗
# 46.操作JavaScript的prompt弹窗
if __name__=="__main__":
driver = webdriver.Chrome()
one = CoreWeb(driver)
#one.setUp("http://test.credit.hexinxiaodai.com/")
one.operation("21","setUp","id","sdsds","http://test.credit.hexinxiaodai.com/login")
one.operation("用户名","input","id","username","admin")
one.operation("密码",'input','id',"password","1111Aa")
one.operation("登陆",'click','xpath',"//input[@value='登录']")
one.operation('','click','xpath',"//ul[@id='tt']/li/div")
one.operation("",'click','link text',"进件管理")
one.operation(" 产品","click","xpath",'//*[@id="searchFromId"]/table[1]/tbody/tr[1]/td[4]/span/span/span')
one.operation("选择","click","xpath","//*[text()='和享贷']")
#