python+selenium十:selenium的二次封装

from selenium import webdriverfrom selenium.webdriver.support.wait import WebDriverWaitfrom selenium.webdriver.support import expected_conditions as ECfrom selenium.webdriver.support.select import Selectfrom selenium.webdriver.common.action_chains import ActionChainsfrom selenium.webdriver.common.by import By# BY的用法# driver.find_element("id", "kw")# driver.find_element(By.ID, "kw")

class Bace():    ‘‘‘基于原生的selenium做二次封装‘‘‘

def __init__(self, driver:webdriver.Firefox):  # driver:webdriver.Firefox:映射driver 为webdriver.Firefox        self.driver = driver        self.timeout = 10        self.t = 0.5

def find(self, locator, value=‘‘):        ‘‘‘  定位到元素,返回元素对象,没定位到,Timeout异常 loctor 传元祖,如("id", "kw")  ‘‘‘        if not isinstance(locator, tuple):            print(‘locator参数类型错误,必须传元祖类型:loc = ("id", "value1")‘)        else:            print(f"正在定位元素信息:定位方式->{locator[0]}, 元素值->{locator[1]},value值->{value}")            if value != ‘‘:  # value值定位                ele = WebDriverWait(self.driver, self.timeout, self.t).until(EC.text_to_be_present_in_element_value(locator, value))                return ele            else:  # 默认为此常规定位方法                ele = WebDriverWait(self.driver, self.timeout, self.t).until(EC.presence_of_element_located(locator))                if ele:                    return ele                else:                    print(f"定位失败:定位方式->{locator[0]}, value值->{locator[1]}")                    return False

def finds(self, locator, value=‘‘):        ‘‘‘ 定位到元素,返回元素对象,没定位到,Timeout异常  loctor 传元祖,如("id", "kw")  ‘‘‘        if not isinstance(locator, tuple):            print(‘locator参数类型错误,必须传元祖类型:loc = ("id", "value1")‘)        else:            print(f"正在定位元素信息:定位方式->{locator[0]}, 元素值->{locator[1]},value值->{value}")            if value != ‘‘:  # value值定位                eles = WebDriverWait(self.driver, self.timeout, self.t).until(EC.text_to_be_present_in_element_value(locator, value))                return eles            else:  # 默认为此常规定位方法                eles = WebDriverWait(self.driver, self.timeout, self.t).until(EC.presence_of_element_located(locator))                if eles:                    return eles                else:                    print(f"定位失败:定位方式->{locator[0]}, value值->{locator[1]}")                    return []

def sendKeys(self, locator, text):        try:            self.find(locator).send_keys(text)        except:            print(f"输入 {text} 失败")

def click(self, locator):        try:            self.find(locator).click()        except:            print("点击失败")

def clear(self, locator):        try:            self.find(locator).clear()        except:            print("清空内容失败")

def isSelected(self, locator, Type=‘‘):        ‘‘‘ 判断元素是否被选中,返回bool值 及点(选中/取消选中)‘‘‘        ele = self.find(locator)        try:            if Type == ‘‘:  # 如果type参数为空,返回元素是否为选中状态,True/False (默认)                r = ele.is_selected()                return r            elif Type == ‘click‘:  # 如果type参数为click,执行元素的点击操作                ele.click()            else:                print(f"type参数 {Type} 错误,仅可为click或‘‘")        except:            return False

def isElementExist(self, locator):        ‘‘‘ 判断单个元素是否在DOM里面 (是否存在)‘‘‘        try:            self.find(locator)            return True        except:            return False

def isElementExists(self, locator):        ‘‘‘ 判断一组元素是否在DOM里面 (是否存在),若不存在,返回一个空的list‘‘‘        eles = self.finds(locator)        n = len(eles)        if n == 0:            return False        elif n == 1:            return True        else:            print(f"定位到元素的个数:{n}")            return True

def title(self, title, Type=‘contains‘):        ‘‘‘  根据传入的type类型判断title  ‘‘‘        try:            if Type == ‘is‘:  # 判断当前网页title名为title   返回bool值                result = WebDriverWait(self.driver, self.timeout, self.t).until(EC.title_is(title))                return result            elif Type == ‘contains‘:  # 判断当前网页title名含title   返回bool值 (默认)                result = WebDriverWait(self.driver, self.timeout, self.t).until(EC.title_contains(title))                return result            else:                print(f"type参数 {Type} 错误,仅可为is、contains")        except:            return False

def in_element(self, locator, value, Type=‘text‘):        ‘‘‘  根据传入的type判断内容是否在指定元素里面  ‘‘‘        if not isinstance(locator, tuple):            print(‘locator参数类型错误,必须传元祖类型:loc = ("id", "value1")‘)        try:            if Type == ‘text‘:  # 判断当前获取到的text含value   返回bool值 (默认)                result = WebDriverWait(self.driver, self.timeout, self.t).until(EC.text_to_be_present_in_element(locator, value))                return result            elif Type == ‘value‘:  # 判断当前获取到的value含value 返回bool值, value为空字符串,返回False                result = self.find(locator, value)                return result            else:                print(f"type参数 {Type} 错误,仅可使用text或value属性定位")                return False        except:            return False

def alert(self, timeout=3, Type=‘‘):        ‘‘‘ 根据传入的type判断alert弹窗及操作 ‘‘‘        result = WebDriverWait(self.driver, timeout, self.t).until(EC.alert_is_present())        try:            if Type == ‘‘:  # 判断alert是否存在,如果有,就返回alert对象 (默认)                if result:                    return result                else:                    print("alert不存在")                return False            elif Type == ‘yes‘:  # 执行alert的确定按钮                result.accept()            elif Type == ‘no‘:  # 执行alert的取消按钮                result.dismiss()            else:                print(f"type参数 {Type} 错误,仅可为yes、no、或‘‘")        except:            return False

def get(self, locator, Type=‘text‘, name=‘‘):        ‘‘‘ 根据传入的type判断获取指定的内容 (title、text、attribute)        type==attribute:  获取元素属性  name:属性  className、name、text、value···  ‘‘‘        try:            if Type == ‘title‘:  # 获取当前网页 title                return self.driver.title            elif Type == ‘text‘:  # 获取元素文本值(默认)                return self.find(locator).text            elif Type == ‘attribute‘:  # 获取当前元素属性                return self.find(locator).get_attribute(name)            else:                print(f"给的type参数 {Type} 错误,仅可用title、text、attribute")        except:            print(f"获取 {Type} 值失败")            return ‘‘

def select(self, locator, value, Type=‘index‘):        ‘‘‘  下拉选项框 根据传入的type值判断(index、value、text)  ‘‘‘        element = self.find(locator)  # 定位select这一栏        try:            if Type == ‘index‘:  # 用下标选择 (默认)                Select(element).select_by_index(value)            elif Type == ‘value‘:  # 根据value值选择                Select(element).select_by_value(value)            elif Type == ‘text‘:  # 根据选项的文本内容选择                Select(element).select_by_visible_text(value)            else:                print(f"给的type参数 {Type} 错误,仅可为:int、text、value")        except:            print(f"根据 {value} 操作下拉框失败")

def iframe(self, id_index_locator):        ‘‘‘ 常规切换 iframe‘‘‘        try:            if isinstance(id_index_locator, int):  # 如果传入的是数字,则以该数字为下标取值                self.driver.switch_to.frame(id_index_locator)            elif isinstance(id_index_locator, str):  # 如果传入的是字符串,则用iframe名字取值                self.driver.switch_to.frame(id_index_locator)            elif isinstance(id_index_locator, tuple):  # 如果是元祖,则根据传入的locator取值                ele = self.find(id_index_locator)                self.driver.switch_to.frame(ele)        except:             print("iframe切换异常")

def handle(self, value):        ‘‘‘ 句柄切换,index、句柄名 ‘‘‘        try:            if isinstance(value, int):  # 切换到该下标对应的窗口                handles = driver.window_handles                self.driver.switch_to.window(handles[value])            elif isinstance(value, str):  # 切换到该句柄名称对应的窗口                self.driver.switch_to.window(value)            else:                print(f"传入的type参数 {value} 错误,仅可传int、str")        except:            print(f"根据 {value} 获取句柄失败")

def move_to_element(self, locator):        ‘‘‘ 鼠标悬停操作 ‘‘‘        try:            ele = self.find(locator)            ActionChains(self.driver).move_to_element(ele).perform()        except:            print("鼠标悬停操作失败")            return False    ‘‘‘==============================js与jQuery相关=====================================‘‘‘

def js_focus_element(self, locator):        ‘‘‘ 聚焦元素 ‘‘‘        target = self.find(locator)        self.driver.execute_script("arguments[0].scrollIntoView();", target)

def js_scroll_top(self):        ‘‘‘ 滚动到顶部 ‘‘‘        js = "window.scrollTo(0,0)"        self.driver.execute_script(js)

def js_scroll_end(self, x=0):        ‘‘‘ 滚动到底部 ‘‘‘        js = f"window.scrollTo({x},document.body.scrollHeight)"        self.driver.execute_script(js)

def js_find(self, action):        ‘‘‘  js查找元素,并做相应操作(默认id属性) 输入值:value=‘XXX‘  点击:click() ‘‘‘        js = f"document.getElementById(“id”).{action}"        self.driver.execute_script(js)

def js_finds(self, Type, element, index, action):        ‘‘‘   js查找元素,并做相应操作   输入值:value=‘XXX‘     点击:click()        js定位仅可为:id、Name、TagName、ClassName、Selector(CSS) ‘‘‘        list = [‘Name‘, ‘TagName‘, ‘ClassName‘, ‘Selector‘]        if type in list:            print(f"正在执行js操作:定位方式->{Type}, 元素值->{element}, 下标值->{index}, 执行操作->{action}")            if type == ‘Selector‘:                js = f‘document.query{Type}All("{element}"){index}.{action}‘            else:                js = f‘document.getElementsBy{Type}({element})[{index}].{action};‘            self.driver.execute_script(js)        else:            print(f"type参数 {Type} 错误,js定位仅可为:‘Name‘、‘TagName‘、‘ClassName‘、‘Selector‘(CSS)")

def js_readonly(self, idElement, value):        ‘‘‘ 去掉只读属性,并输入内容 一般为id ‘‘‘        js = f‘document.getElementById({idElement}).removeAttribute("readonly");document.getElementById({idElement}).value="{value}"‘        driver.execute_script(js)

def js_iframe(self, Type, element, action, index=‘‘):        ‘‘‘  Js处理iframe   无需先切换到iframe上,再切回来操作        输入值:value=‘‘         点击:click()           type=id时,index=‘‘  ‘‘‘        js = f‘document.getElementBy{Type}({element}){index}.contentWindow.document.body.{action}‘        driver.execute_script(js)    ‘‘‘        jquery = ‘$(CSS).val("XXX");‘   # 根据css语法定位到元素,输入内容        jquery = ‘$(CSS).val(‘‘);‘      # 清空        jquery = ‘$(CSS).click();‘      # 点击        driver.execute_script(jquery)    ‘‘‘

# def switch_alert(self):    #     ‘‘‘ 获取alert弹窗 ‘‘‘    #     r = self.is_alert()    #     if not r:    #         print("alert不存在")    #     else:    #         return r

# def is_title(self, title):    #     ‘‘‘判断当前title名为title   返回bool值‘‘‘    #     try:    #         result = WebDriverWait(self.driver, self.timeout, self.t).until(EC.title_is(title))    #         return result    #     except:    #         return False    # def is_title_contains(self, title):    #     ‘‘‘判断当前title名含title   返回bool值‘‘‘    #     try:    #         result = WebDriverWait(self.driver, self.timeout, self.t).until(EC.title_contains(title))    #         return result    #     except:    #         return False

# def is_text_in_element(self, locator, _text=‘‘):    #     ‘‘‘判断当前获取到的text含_text=‘‘   返回bool值‘‘‘    #     if not isinstance(locator, tuple):    #         print(‘locator参数类型错误,必须传元祖类型:loc = ("id", "value1")‘)    #     try:    #         result = WebDriverWait(self.driver, self.timeout, self.t).until(EC.text_to_be_present_in_element(locator, _text))    #         return result    #     except:    #         return False    # def is_value_in_element(self, locator, _value=‘‘):    #     ‘‘‘返回bool值, value为空字符串,返回False‘‘‘    #     if not isinstance(locator, tuple):    #         print(‘locator参数类型错误,必须传元祖类型:loc = ("id", "value1")‘)    #     try:    #         result = WebDriverWait(self.driver, self.timeout, self.t).until(EC.text_to_be_present_in_element_value(locator, _value))    #         return result    #     except:    #         return False

# def get_title(self):    #     ‘‘‘获取title‘‘‘    #     return self.driver.title    # def get_text(self, locator):    #     ‘‘‘获取文本‘‘‘    #     try:    #         t = self.find(locator).text    #         return t    #     except:    #         print("获取text失败,返回‘‘ ")    #         return ""    # def get_attribute(self, locator, name):    #     ‘‘‘获取属性‘‘‘    #     try:    #         element = self.find(locator)    #         return element.get_attribute(name)    #     except:    #         print("获取%s属性失败,返回‘‘ "%name)    #         return ""

# def select_by_index(self, locator, index=0):    #     ‘‘‘通过索引,index是索引第几个,从0开始,默认选第一个‘‘‘    #         element = self.find(locator)  # 定位select这一栏    #         Select(element).select_by_index(index)

# def select_by_value(self, locator, value):    #     ‘‘‘通过value属性‘‘‘    #     element = self.find(locator)    #     Select(element).select_by_value(value)

# def select_by_text(self, locator, text):    #     ‘‘‘通过文本值定位‘‘‘    #     element = self.find(locator)    #     Select(element).select_by_visible_text(text)

# def switch_handle_window_name(self, window_name):    #     ‘‘‘ 根据句柄名字切换句柄 ‘‘‘    #     self.driver.switch_to.window(window_name)    # def switch_handle_index(self, index):    #     ‘‘‘  根据句柄下标切换句柄  ‘‘‘    #     handles = driver.window_handles    #     self.driver.switch_to.window(handles[index])

# def js_find(self, action):    #     ‘‘‘    #     输入值:value=‘XXX‘     点击:click()    #     ‘‘‘    #     print("正在执行js操作,操作行为:%s"%action)    #     js = "document.getElementById(“id”).%s"%action    #     self.driver.execute_script(js)

if __name__ == "__main__":    driver = webdriver.Firefox()    driver.get("")    zentao = Base(driver)    # loc1 = (By.ID, "account")    # loc2 = (By.CSS_SELECTOR, "[name=‘password‘]")    # loc3 = (By.XPATH, "//*[@id=‘submit‘]")

loc1 = ("id", "account")    loc2 = ("css selector", "[name=‘password‘]")    loc3 = ("xpath", "//*[@id=‘submit‘]")    zentao.sendKeys(loc2, 123)

zentao.move_to_element(loc3)

原文地址:https://www.cnblogs.com/dwdw/p/9998660.html

时间: 2024-08-03 01:08:25

python+selenium十:selenium的二次封装的相关文章

selenium + python自动化测试unittest框架学习(五)webdriver的二次封装

因为webdriver的api方法很长,再加上大多数的定位方式是以xpath方式定位,更加让代码看起来超级长,为了使整体的代码看起来整洁,对webdriver进行封装,学习资料来源于虫师的<selenium +  python自动化测试>一书. 在与测试用例文件夹同一目录下新建一个文件夹package,用来放置封装方法的模块文件 我们将webdriver二次封装的文件命名为location.py from selenium import webdriver from test_case.pub

Selenium二次封装-Python版本

1 from selenium import webdriver 2 from selenium.webdriver.support.wait import WebDriverWait 3 from selenium.webdriver.support import expected_conditions as EC 4 from selenium.webdriver.common.by import By 5 from selenium.webdriver.common.action_chai

基于python语言的Selenium自动化测试

  一.Selenium简介 Selenium是一个web自动化应用测试工具,还支持所有的web的管理任务自动化.并且开源免费,也是一个web自动化轻量级框架.它支持多种浏览器.跨平台.跨操作系统.支持多种编程语言编写脚本,还能进行分布式测试用例的执行. Selenium经历三个版本,Selenium1.Selenium2.Selenium3.它不是单独的一个工具,而是由一些类库.插件.工具等组合而成.主要的工具: Selenium IDE:Selenium IDE是嵌入火狐浏览器中的一个插件,

Python爬虫之selenium的使用(八)

Python爬虫之selenium的使用 一.简介 二.安装 三.使用 一.简介 Selenium 是自动化测试工具.它支持各种浏览器,包括 Chrome,Safari,Firefox 等主流界面式浏览器,如果你在这些浏览器里面安装一个 Selenium 的插件,那么便可以方便地实现Web界面的测试.Selenium 支持这些浏览器驱动.Selenium支持多种语言开发,比如 Python,Java,C,Ruby等等. 二.安装 1.安装selenium pip3 install seleniu

Python进阶(十二)----re模块

Python进阶(十二)----re模块 一丶re模块 ? re模块是python将正则表达式封装之后的一个模块.正则表达式模式被编译成一系列的字节码,然后由用C编写的匹配引擎执行. #正则表达式: 从一串字符中,找出你想要的字符串. import re ### 单个元字符的匹配 # \W 除了数字 ,字母,中文, 下划线 print(re.findall('\W','dsadas1231 +1-+2*/,.')) # \w 匹配中文,数字,字母,下划线 print(re.findall('\w

Python进阶(十六)----面向对象之~封装,多态,鸭子模型,super原理(单继承原理,多继承原理)

Python进阶(十六)----面向对象之~封装,多态,鸭子模型,super原理(单继承原理,多继承原理) 一丶封装 , 多态 封装: ? ? ? ? ? ?将一些东西封装到一个地方,你还可以取出来 ? ? ? ? ? ?类设置静态属性, 设置一些方法 或者 对象, 对象可以在其对象封装一些属性 多态: ? ? ? ? ? ?python默认支持多态, 多态指的是一种事务具有多种形态 ? ? ? ? ? ?1.多态可以增加代码的灵活度: ? ? ? ? ? ?2.以继承和重写父类方法为前提: ?

初学 Python(十二)——高阶函数

初学 Python(十二)--高阶函数 初学 Python,主要整理一些学习到的知识点,这次是高阶函数. #-*- coding:utf-8 -*- ''''' 话说高阶函数: 能用函数作为参数的函数 称为高阶函数 ''' #函数作参 def f(x): return x*x #map函数为内置函数,意思为将第二个参数的list作用到f函数中 #最后的结果为一个list print map(f,[1,2,3,4,5]) #reduce函数为内置函数,意思将第二参数的序列作用到add函数值 #将结

Python爬虫使用Selenium+PhantomJS抓取Ajax和动态HTML内容

在上一篇python使用xslt提取网页数据中,要提取的内容是直接从网页的source code里拿到的. 但是对于一些Ajax或动态html, 很多时候要提取的内容是在source code找不到的,这种情况就要想办法把异步或动态加载的内容提取出来. python中可以使用selenium执行javascript,selenium可以让浏览器自动加载页面,获取需要的数据.selenium自己不带浏览器,可以使用第三方浏览器如Firefox, Chrome等,也可以使用headless浏览器如P

Python 语言搭建SELENIUM测试环境,搭建过程记录。

第一步,安装Python: 第二步,安装SetupTools: 第三步,安装Pip: 第四步,安装selenium(for python) 第五步,新建第一个基于Firefox的测试用例 上述 只是步骤,具体内容,明天补充,睡觉 咯--- Python 语言搭建SELENIUM测试环境,搭建过程记录.