• selinium webdriver二次封装


    背景:本文是小编结合selenium+webdriver编写的一些封装方法,目的是更简单的调用使用这些方法,主要封装了下面几种:
    1.封装了expected_conditions(selenium自带判断方法) + WebDriverWait(显式等待)封装了一些常用的元素定位和判断方法,从而达到让脚本更稳定的效果
    2.封装了ActionChains鼠标事件,可以快速调用鼠标悬停操作
    3.封装了xlrd 操作excel 的方法,可以读取excel数据
    4.封装了JS脚本定位元素的方法,目的是为了在常规方法定位不到情况下补漏
    
    #coding:utf-8
    
    from selenium import webdriver
    from selenium.webdriver.common.action_chains import ActionChains #导入鼠标事件
    from selenium.webdriver.support.select import Select#导入select方法
    from selenium.webdriver.support import expected_conditions as EC #导入EC判断方法
    from selenium.webdriver.support.wait import WebDriverWait# 导入 显示等待
    from selenium.common.exceptions import * #导入所有异常类
    import time
    import os
    import xlrd
    
    class BasePage(object):
        '''基于原生的selenium框架做了二次封装'''
    
        def __init__(self,driver):
            '''初始化driver'''
            self.driver=driver
    
        def open(self,url):
            '''输入网址,并页面最大化'''
            driver.get(url)
            driver.maximize_window()
    
        def find_element(self,locator):
            '''定位元素,参数locator是元祖类型'''
            element=WebDriverWait(self.driver,timeout=10).until(EC.presence_of_element_located(locator))
            return element
    
        def find_elements(self,locator):
            '''定位一组元素,参数locator是元祖类型'''
            elements=WebDriverWait(self.driver,timeout=10).until(EC.presence_of_all_elements_located(locator))
            return elements
    
        def send_keys(self,locator,text,is_clear=True):
            '''输入文本方法'''
            element=self.find_element(locator)
            if is_clear==True:element.clear()
            element.send_keys(text)
    
        def click(self,locator):
            '''点击方法'''
            element=self.find_element(locator)
            element.click()
    
        def get_text(self,locator):
            '''查看页面元素的文本是否存在'''
            t=self.find_element(locator).text
            return t
    
        def is_located(self,locator):
            '''是否定位到元素'''
            result=WebDriverWait(driver,timeout=10).until(EC.presence_of_element_located(locator))
            return result
    
        def is_exists_element(self,locator):
            '''判断元素是否存在'''
            try:
                self.is_located(locator)
                return True
            except:
                return False
    
        def get_title(self):
            '''获取title'''
            return driver.title
    
        def is_alert_persent(self):
            '''判断页面是否有alert弹出框,有返回alert,没有返回False'''
            result=WebDriverWait(driver,timeout=10).until(EC.alert_is_present())
            return result
    
        def mouse_move(self,locator):
            '''鼠标悬停操作'''
            element=self.find_element(locator)
            ActionChains(self.driver).move_to_element(element).perform()
    
        def is_ifame(self,locator):
            '''判断是否存在ifame'''
            boolean=WebDriverWait(driver,timeout=10).until(EC.frame_to_be_available_and_switch_to_it(locator))
            return boolean
    
        def js_window_bom(self):
            '''通过执行js脚本方式将页面置底,参数左边代表左右移动,右边代表上下'''
            js="window.scrollTo(0,10000);"
            driver.execute_script(js)
    
        def js_window_top(self):
            '''通过执行js脚本方式将页面置顶,参数左边代表左右移动,右边代表上下'''
            js="window.scrollTo(0,0);"
            driver.execute_script(js)
    
        def js_element_top(self,locator):
            '''通过js脚本方式将指定元素置顶'''
            try:
                self.target=self.find_element(locator)
                self.driver.execute_script("arguments[0].scrollIntoView();",  self.target)
            except Exception as msg:
                print("没有定位到该元素,无法置顶:"+str(msg))
    
       def read_excel_values(self,path,nrows,ncols):
            '''读取指定excel的某个值'''
            excel=xlrd.open_workbook(path)#打开指定路径excel文件
            sheet=excel.sheet_by_name("Sheet1")#指定sheet
            data=sheet.cell_value(nrows,ncols)#读取指定行列的值
            return data
    
        def read_excel_nrows(self,path,nrows):
            '''读取指定一行数据'''
            excel=xlrd.open_workbook(path)#打开指定路径excel文件
            sheet=excel.sheet_by_name("Sheet1")#指定sheet
            data = sheet.row_values(nrows)#读取指定某一行数据
            return data
       def openfile(filePath):
       '''使用内置函数open,打开文件读取文件'''
          with open(filePath,"r", encoding="utf-8") as a:
           return a.read()
      
       def openWrite(path,anyStr):
       '''使用内置函数open,打开文件写入文本到文件'''
          with open(path,"w",encoding="utf-8") as a:  
             a.write(anyStr)
    
      
      
    if __name__=="__main__": 
      driver=webdriver.Chrome() 
      my_driver=BasePage(driver) 
      my_driver.open("https://www.baidu.com") 
      my_driver.send_keys(("id","kw"),"刘德华") print("1") 
      my_driver.click(("id","su")) print("4")
  • 相关阅读:
    VS2012 启动性能分析导致电脑重启问题解决方法
    C# sqlite no such table 问题
    C# webkit内核 网页填表
    smali注入log输出信息
    Android Studio 连接夜神模拟器
    chrome 所有请求返回内容中查找字符串 CTRL+SHIFT+F
    C# WinForm调用控制台窗口Console 显示信息
    C# 判断txt文件编码格式
    C# 利用DotRas 操作adsl
    QQ gtk,bkn算法
  • 原文地址:https://www.cnblogs.com/onelove1/p/13106031.html
Copyright © 2020-2023  润新知