美文网首页
python+selenium

python+selenium

作者: 心悦飞飞 | 来源:发表于2018-08-21 20:56 被阅读0次
# 用于切换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()












相关文章