python+selenium常用方法封装

demo如下:

#encoding=utf-8
from selenium import webdriver
from Util.KeyBoardUtil import KeyBoardKeys
from Util.ClipboardUtil import Clipboard
import time

#定义全局变量driver
driver = None


def open(browserName):
    #打开浏览器
    global driver,waitUtil
    try:
        if browserName.lower() == "ie":
            driver = webdriver.Ie()
        elif browserName.lower == "chrome":
            #创建Chrome浏览器的一个Options实例对象
            driver = webdriver.Chrome()
        else:
            driver = webdriver.Firefox()
    except Exception as e:
        raise e

def visit(url):
    #访问某个网站
    global driver
    try:
        driver.get(url)
    except Exception as e:
        raise e

def close_browser():
    #关闭浏览器
    global driver
    try:
        driver.quit()
    except Exception as e:
        raise e

def sleep(sleepSeconds):
    #强制等待
    try:
        time.sleep(int(sleepSeconds))
    except Exception as e:
        raise e

def clear(locationType,locatorExpression):
    #清空输入框默认内容
    global driver
    try:
        getElement(driver,locationType,locatorExpression).clear()
    except Exception as e:
        raise e

def input_string(locationType,locatorExpression,inputContent):
    #在页面输入框中输入数据
    global driver
    try:
        getElement(driver,locationType,locatorExpression).send_keys(inputContent)
    except Exception as e:
        raise e

def click(locationType,locatorExpression,*args):
    #点击页面元素
    global driver
    try:
        getElement(driver,locationType,locatorExpression).click()
    except Exception as e:
        raise e

def assert_string_in_pagesource(assertString,*args):
    #断言页面源码是否存在某个关键字或关键字符串
    global driver
    try:
        assert assertString in driver.page_source,u"%s not found in page source!" % assertString
    except AssertionError as e:
        raise AssertionError(e)
    except Exception as e:
        raise e

def assert_title(titleStr,*args):
    #断言页面标题是否存在给定的关键字符串
    global driver
    try:
        assert titleStr in driver.title,u"%s not found in page title!" % titleStr
    except AssertionError as e:
        raise AssertionError(e)
    except Exception as e:
        raise e

def getTitle(*args):
    #获取页面标题
    global driver
    try:
        return driver.title
    except Exception as e:
        raise e

def getPageSource(*args):
    #获取页面源码
    global driver
    try:
        return driver.page_source
    except Exception as e:
        raise e

def switch_to_frame(locationType,frameLocatorExpressoin,*args):
    #切换进frame
    global driver
    try:
        driver.switch_to.frame(getElement(driver,locationType,frameLocatorExpressoin))
    except Exception as e:
        print("frame error!")
        raise e

def switch_to_default_content(*args):
    #切换妯frame
    global driver
    try:
        driver.switch_to.default_content()
    except Exception as e:
        raise e

def paste_string(pasteString,*args):
    #模拟Ctrl+V操作
    try:
        Clipboard.setText(pasteString)
        #等待2秒,防止代码执行过快,而未成功粘贴内容
        time.sleep(2)
        KeyBoardKeys.twoKeys("ctrl","v")
    except Exception as e:
        raise e

def press_tab_key(*args):
    #模拟tab键
    try:
        KeyBoardKeys.oneKey("tab")
    except Exception as e:
        raise e

def press_enter_key(*args):    
    #模拟enter键
    try:
        KeyBoardKeys.oneKey("enter")
    except Exception as e:
        raise e

def maximize(*args):   
    #窗口最大化
    global driver
    try:
        driver.maximize_window()
    except Exception as e:
        raise e

def capture(file_path):  #截图
    try:
        driver.save_screenshot(file_path)
    except Exception as e:
        raise e


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值