# 用于切换window 窗口用的, 通过页面标题 或者 页面相关信息文
def switch_window(self,page_keywords):
# window_name: The name or window handle of the window to switch to
for x in self.driver.window_handles:
self.driver.switch_to.window(x)
if page_keywords in self.driver.page_source:
print(self.driver.title)
print(">>>Switch window is successful<<<")
break
else:
print("not find the windows....."+page_keywords)
# 构造方法
def __init__(self, driver):
self.driver = driver
# 打开浏览器
def open_browser(self, url):
self.driver.get(url)
self.driver.maximize_window()
self.driver.set_page_load_timeout(5)
#关闭浏览器
def close_browser(self):
self.driver.quit()
# 查找页面元素的方法(有多少个,就返回多个个, 返回值是一个列表)
def find_elements(self, locator_type, locator_value):
if locator_type is const.ID:
return self.driver.find_elements_by_id(locator_value)
elif locator_type is const.NAME:
return self.driver.find_elements_by_name(locator_value)
elif locator_type is const.TAGNAME:
return self.driver.find_elements_by_tag_name(locator_value)
elif locator_type is const.CLASSNAME:
return self.driver.find_elements_by_class_name(locator_value)
elif locator_type is const.LINKTEXT:
return self.driver.find_elements_by_link_text(locator_value)
elif locator_type is const.CSSSELECTOR:
return self.driver.find_elements_by_css_selector(locator_value)
elif locator_type is const.PARTIALLINKTEXT:
return self.driver.find_elements_by_partial_link_text(locator_value)
# 查找页面单个元素
def find_element(self, locator_type, locator_value):
element = self.find_elements(locator_type, locator_value)
try:
if len(element) == 1:
self.log.log_info(len(element))
self.log.log_info("you have found the specified element...")
return element[0]
elif len(element) > 1:
self.log.log_info(len(element))
self.log.log_info("find more than one element ... ")
return element[0]
elif len(element) < 1:
self.log.log_info("not find the specified element ... " + locator_value)
except Exception, e:
self.log.log_info(e)
pass
# 终极方法
def operation(self, element, element_type, locator_type, related_value, *input_information):
if element_type is const.INPUT:
self.log.log_info(element+",will be send "+input_information[0])
self.find_element(locator_type, related_value).send_keys(input_information)
elif element_type is const.CLICK:
self.find_element(locator_type, related_value).click()
elif element_type is const.FRAME:
self.driver.switch_to.frame(0)
elif element_type is const.WINDOW:
self.switch_window(related_value)
# -*- coding: utf-8 -*-
# 通过自定义类实现常量功能。
# 这要求符合“命名全部为大写”和“值一旦被绑定便不可再修改”这两个条件
# 定义元素的方式常量表
ID = "id"
NAME = "name"
XPATH = "xpath"
TAGNAME = "tagName"
LINKTEXT = "linkText"
CLASSNAME = "className"
CSSSELECTOR = "cssSelector"
PARTIALLINKTEXT = "partialLinkText"
# 定位元素类型的常量表
INPUT = "input"
CLICK = "click"
SELECT = "select"
FRAME = "frame"
WINDOW = "window"
def findelement(self, how, what):
element = WebDriverWait(self.driver, 30).until(lambda x: x.find_element(by=how, value=what))
return element
def findelements(self, how, what):
elements = WebDriverWait(self.driver, 30).until(lambda x: x.find_elements(by=how, value=what))
return elements
操作框架frame
def frame(self, basekey):
for i in range(10):
if basekey not in self.driver.page_source:
self.driver.switch_to.frame(0)
def defaultContent(self):
self.driver.switch_to.default_content()
python+selenium
©著作权归作者所有,转载或内容合作请联系作者
- 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
- 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
- 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
推荐阅读更多精彩内容
- 发现很多人连环境都不会搭,虽然这个问题没有什么技术含量,但博主也决定写点东西给那些环境都不会搭建的小白。 关于se...
- 发现太多人不会用等待了,博主今天实在是忍不住要给大家讲讲等待的必要性。 很多人在群里问,这个下拉框定位不到、那个弹...
- 在用selenium webdriver 编写web页面的自动化测试代码时,可能需要执行一些javascript代...
- 1、基本操作类: 2、定位: 3、鼠标 4、键盘 使用特殊按键和组合按键需要import相关文件,代码如下: 普通...
- 操作环境:Windows 10 + Python 2.75 首先我们先来安装Selenium: 然后给电脑安装Ch...