零碎知识 Selenium 二次封装-Python 版本

大海 · 2022年04月27日 · 最后由 啊神 回复于 2022年04月28日 · 4965 次阅读
# !/usr/bin/python
# -*- coding: utf-8 -*-

"""
selenium常用方法二次封装文件
"""


from selenium import webdriver
from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
from selenium.webdriver.common.action_chains import ActionChains
from selenium.webdriver.support.select import Select




class PySelenium(object):

    '''
    初始化,实例化浏览器驱动对象
    '''
    def __init__(self, browser='ff'):
        driver = None
        if browser == 'ff' or browser == 'firefox':
            driver = webdriver.Firefox()
        elif browser == "chrome" or browser == "Chrome":
            option = webdriver.ChromeOptions()
            option.add_argument("--start-maximized")
            driver = webdriver.Chrome(chrome_options=option)
        elif browser == 'ie' or browser == 'internet explorer':
            driver = webdriver.Ie()
        elif browser == "opera":
            driver = webdriver.Opera()
        elif browser == "phantomjs":
            driver = webdriver.PhantomJS()
        elif browser == 'edge':
            driver = webdriver.Edge()

        try:
            self.driver = driver
        except Exception:
            # 手动抛出异常
            raise NameError(
                "Not found %s browser,You can enter 'ie', 'ff', 'opera', 'phantomjs', 'edge' or 'chrome'." % browser)


    def elementWait(self, css, secs=5):
        '''
        设置元素等待
        '''
        # 判断表达式是否包含指定字符
        if "=>" not in css:
            raise NameError("Positioning syntax errors, lack of '=>'.")

        # 提取元素定位方式和定位表达式
        by = css.split("=>")[0]
        value = css.split("=>")[1]
        messages = 'Element: {0} not found in {1} seconds.'.format(css, secs)

        if by == "id":
            WebDriverWait(self.driver, secs, 1).until(EC.presence_of_element_located((By.ID, value)), messages)
        elif by == 'name':
            WebDriverWait(self.driver, secs, 1).until(EC.presence_of_element_located((By.NAME, value)), messages)
        elif by == "class":
            WebDriverWait(self.driver, secs, 1).until(EC.presence_of_element_located((By.CLASS_NAME, value)), messages)
        elif by == "link_text":
            WebDriverWait(self.driver, secs, 1).until(EC.presence_of_element_located((By.LINK_TEXT, value)), messages)
        elif by == "xpath":
            WebDriverWait(self.driver, secs, 1).until(EC.presence_of_element_located((By.XPATH, value)), messages)
        elif by == "css":
            WebDriverWait(self.driver, secs, 1).until(EC.presence_of_element_located((By.CSS_SELECTOR, value)), messages)
        else:
            raise NameError(
                "Please enter the correct targeting elements,'id','name','class','link_text','xpath','css'.")


    def getElement(self, css):
        '''
        获取指定元素对象
        表达式:  by=>value (by为定位方式,value为定位方式的表达式,例如:按照id定位某个元素,id=>"#")
        '''
        if "=>" not in css:
            raise NameError("Positioning syntax errors, lack of '=>'.")

        by = css.split("=>")[0]
        value = css.split("=>")[1]

        if by == 'id':
            element = self.driver.find_element(By.ID, value)
        elif by == 'name':
            element = self.driver.find_element(By.NAME, value)
        elif by == 'class':
            element = self.driver.find_element(By.CLASS_NAME, value)
        elif by == 'link_text':
            element = self.driver.find_element(By.LINK_TEXT, value)
        elif by == 'xpath':
            element = self.driver.find_element(By.XPATH, value)
        elif by == 'css':
            element = self.driver.find_element(By.CSS_SELECTOR, value)
        elif by == "tag":
            element = self.driver.find_element(By.TAG_NAME, value)
        else:
            raise NameError("Please enter the correct targeting elements,'id','name','class','link_text','xpath','css'.")
        return element

    def getElements(self, css):
        """
        Judge elements positioning way, and returns the elements.

        Usage:
        driver.get_element('id=>kw')
        """
        if "=>" not in css:
            raise NameError("Positioning syntax errors, lack of '=>'.")

        by = css.split("=>")[0].strip()
        value = css.split("=>")[1].strip()

        if by == "id":
            elements = self.driver.find_element(By.ID, value)
        elif by == "name":
            elements = self.driver.find_element(By.NAME, value)
        elif by == "class":
            elements = self.driver.find_element(By.CLASS_NAME, value)
        elif by == "link_text":
            elements = self.driver.find_element(By.LINK_TEXT, value)
        elif by == "xpath":
            elements = self.driver.find_element(By.XPATH, value)
        elif by == "css":
            elements = self.driver.find_element(By.CSS_SELECTOR, value)
        elif by == "tag":
            elements = self.driver.find_element(By.TAG_NAME, value)
        else:
            raise NameError(
                "Please enter the correct targeting elements,'id','name','class','link_text','xpath','css'.")
        return elements



    def openUrl(self, url):
        '''
        请求/打开指定链接
        '''
        self.driver.get(url)


    def maxWindows(self):
        '''
        窗口最大化
        '''
        self.driver.maximize_window()


    def setWindowsSize(self, wide, high):
        '''
        设置窗口指定宽高
        '''
        self.driver.set_window_size(width=wide, height=high)


    def addText(self, css, massage):
        '''
        添加文本到input
        '''
        self.elementWait(css)
        input = self.getElement(css)
        input.send_keys(massage)


    def clear(self, css):
        '''
        清空input中的文本
        '''
        self.elementWait(css)
        self.getElement(css).clear()


    def click(self, css):
        '''
        鼠标左键单击
        '''
        self.elementWait(css)
        self.getElement(css).click()


    def rightClick(self, css):
        '''
        鼠标右键单击
        '''
        self.elementWait(css)
        ActionChains(self.driver).context_click(self.getElement(css)).perform()


    def moveToTargetElement(self, css):
        '''
        移动鼠标到指定元素(默认在元素的中间位置)
        '''
        self.elementWait(css)
        ActionChains(self.driver).move_to_element(self.getElement(css)).perform()


    def moveToTargetElementWithOffset(self, css, xoffset, yoffset):
        '''
        移动鼠标到指定元素,并且指定位于元素的x,y偏移量(偏移量相对于元素的左上角)
        '''
        self.elementWait(css)
        ActionChains(self.driver).move_to_element_with_offset(self.getElement(css), xoffset, yoffset).perform()


    def doubleClick(self, css):
        '''
        鼠标左键双击
        '''
        self.elementWait(css)
        ActionChains(self.driver).double_click(self.getElement(css)).perform()


    def dragAndDropToElement(self, sourceCss, targetCss):
        '''
        拖拽元素到指定元素处
        '''
        self.elementWait(sourceCss)
        self.elementWait(targetCss)
        ActionChains(self.driver).drag_and_drop(self.getElement(sourceCss),self.getElement(targetCss)).perform()


    def dragAndDropToOffset(self, sourceCss, xoffset, yoffset):
        '''
        拖拽元素指定偏移(该偏移是相对于当前鼠标的坐标偏移量)
        '''
        self.elementWait(sourceCss)
        ActionChains(self.driver).drag_and_drop_by_offset(self.getElement(sourceCss), xoffset, yoffset).perform()


    def clickLinkText(self, text):
        '''
        鼠标左键点击链接文本
        '''
        self.driver.find_element(By.PARTIAL_LINK_TEXT, text).click()


    def close(self):
        '''
        关闭当前窗口
        '''
        self.driver.close()


    def quit(self):
        '''
        关闭浏览器驱动
        '''
        self.driver.quit()


    def submit(self, css):
        '''
        提交指定表单
        '''
        self.elementWait(css)
        self.getElement(css).submit()


    def F5(self):
        '''
        刷新当前页面,相当于点击F5
        '''
        self.driver.refresh()


    def js(self, javaScript):
        '''
        执行指定的js代码
        '''
        self.driver.execute_script(javaScript)


    def getAttribute(self, css, attr):
        '''
        获取指定元素的某个属性值
        '''
        self.elementWait(css)
        self.getElement(css).get_attribute(attr)


    def getText(self, css):
        '''
        获取指定元素的文本内容,即value属性值
        '''
        self.elementWait(css)
        self.getElement(css).text


    def isDisplay(self, css):
        '''
        判断元素是否可见
        '''
        self.elementWait(css)
        return self.getElement(css).is_displayed()


    def isEnabled(self, css):
        '''
        判断元素是否启用
        '''
        self.elementWait(css)
        return self.getElement(css).is_enabled()


    def isSelected(self, css):
        '''
        判断元素是否选中,一般用于验证checkbox和radio
        '''
        self.elementWait(css)
        return self.getElement(css).is_selected()


    def getTitle(self):
        '''
        获取当前页面的title
        '''
        return self.driver.title


    def getCurrentUrl(self):
        '''
        获取当前页面的url
        '''
        return self.driver.current_url


    def getScreenshot(self, fullFileName):
        '''
        截图,保存到指定路径下文件中
        '''
        self.driver.get_screenshot_as_file(fullFileName)


    def wait(self, secs):
        '''
        全局等待,Implicitly wait.All elements on the page.
        '''
        self.driver.implicitly_wait(secs)


    def alertAccept(self):
        '''
        弹框警告-确认
        '''
        # self.driver.switch_to_alert().accept() 废弃的方式
        self.driver.switch_to.alert.accept()


    def alertDismiss(self):
        '''
        弹框警告-取消
        '''
        # self.driver.switch_to_alert().dismiss() 废弃的方式
        self.driver.switch_to.alert.dismiss()


    def switchFrame(self, css):
        '''
        切换到指定的iframe
        '''
        self.elementWait(css)
        self.driver.switch_to.frame(self.getElement(css))


    def switchFrameOut(self):
        '''
        切换到上一级(iframe)
        '''
        self.driver.switch_to.default_content()


    def openNewWindow(self):
        '''
        打开新页面,并切换当前句柄为新页面的句柄
        (每个页面对应一个句柄handle,可以通过self.driver.window_handles查看所有句柄)
        --当前方法可能存在问题
        '''
        original_windows = self.driver.current_window_handle
        all_handles = self.driver.window_handles
        for handle in all_handles:
            if handle != original_windows:
                self.driver.switch_to.window(handle)



    def waitEleAndSaveExceptionForTimeout(self, css, pictureName):
        '''
        等待元素,默认5秒,每1秒检查一次
        --如果超时则对当前页面截图,以指定的文件名称保存到图片存储目录,并返回false
        '''
        try:
            self.elementWait(css)
            return True
        except Exception as e:
            # from conf.path import PICTUREPATH  # 导入定义的图片存储目录路径
            # pictureFullName = PICTUREPATH + pictureName + '.jpg'
            # self.getScreenshot(pictureFullName)
            return False


    def waitEleAndExceptionForTimeout(self, css):
        '''
        等待元素,10秒,每1秒检查一次
        --如果超时,返回false
        '''
        try:
            self.elementWait(css, secs=10)
            return True
        except Exception as e:
            return False


    def selectByValue(self, css, value):
        '''
        根据指定的值选中相应的下拉列表中的选项
        --如果没有指定的值则抛出异常
        '''
        self.elementWait(css)
        Select(self.getElement(css)).select_by_value(value)


if __name__ == '__main__':
    pySelenium = PySelenium('chrome')

如果觉得我的文章对您有用,请随意打赏。您的支持将鼓励我继续创作!
共收到 3 条回复 时间 点赞

有推动开发给元素加 id 吗,这样才好提高测试稳定性

国文 回复

这个不是他们默认会写的吗?还要我们主动推动啊?

大海 回复

看前端开发人员开发素养,因为有时候不加一些标签对实现并没太大影响。

需要 登录 后方可回复, 如果你还没有账号请点击这里 注册