zoukankan      html  css  js  c++  java
  • Python 封装点击、获取文本等方法工具类

    # coding=utf-8
    import os
    
    from selenium.common.exceptions import ElementNotInteractableException, ElementClickInterceptedException
    from selenium.webdriver import ActionChains
    import time
    from selenium.webdriver.common.keys import Keys
    from base.find_element import FindElement
    
    
    class BasePage(object):
        def __init__(self, driver, url=None):
            self.driver = driver
            self.url = url
            self.element = FindElement(driver)
    
        def open(self):
            self.driver.get(url=self.url)
    
        # 输入文本框
        def send_keys(self, element, text):
            self.clear(element)
            element.send_keys(text)
    
        # 回车
        @staticmethod
        def enter(element):
            element.send_keys(Keys.ENTER)
    
        # 复制
        @staticmethod
        def ctrl_c(element):
            element.send_keys(Keys.CONTROL, 'c')
    
        # 粘贴
        @staticmethod
        def ctrl_v(element):
            element.send_keys(Keys.CONTROL, 'v')
    
        # 刷新
        def refresh(self):
            self.driver.refresh()
    
        # 点击事件
        @staticmethod
        def click(element):
            try:
                element.click()
            except (AttributeError, ElementNotInteractableException, ElementClickInterceptedException) as e:
                raise e
    
        # 上传文件
        @staticmethod
        def upload_file(element, file_path):
            try:
                element.send_keys(file_path)
            except AttributeError as e:
                print(e)
    
        # 清空文本框内容
        @staticmethod
        def clear(element):
            element.send_keys(Keys.CONTROL + 'a')
            element.send_keys(Keys.BACKSPACE)
    
        # 鼠标悬停
        def hover(self, element, link_text):
            ActionChains(self.driver).move_to_element(element).perform()
            time.sleep(5)
            self.driver.find_element_by_link_text(link_text=link_text).click()
    
        # 鼠标悬停事件-js方式实现
        def hover_by_js(self, element):
            js = """
                    var evObj = document.createEvent('MouseEvents'); 
                    evObj.initMouseEvent("mouseover",true, false, window, 0, 0, 0, 0, 0, false, false, false, false, 0, null);
                    arguments[0].dispatchEvent(evObj);
                """
            # more_menu = webElement
            self.driver.execute_script(js, element)
            time.sleep(2)
    
        # 将滚动条拖动到需要显示的元素位置
        def scroll_by_js(self, element):
            self.driver.execute_script("arguments[0].scrollIntoView();", element)
            time.sleep(1)
    
        # 元素定位相互覆盖,导致无法点击,采用js点击
        def click_by_js(self, element):
            self.driver.execute_script("arguments[0].click()", element)
    
        # 获取元素文本内容
        @staticmethod
        def get_element_text(element):
            if element:
                return element.text
            return element
    
        # 获取元素的value值
        @staticmethod
        def get_element_value(element):
            if element:
                return element.get_attribute("value")
            return element
    
        @staticmethod
        def is_enabled(element):
            """Returns whether the element is enabled."""
            return element.is_enabled()
    
        # 判断单个元素是否在DOM里面 (是否存在)
        @staticmethod
        def is_element_exist(element):
            if element:
                return True
            else:
                return False
    
        # 判断元素是否被选中,返回bool值 及点(选中/取消选中)
        @staticmethod
        def is_selected(element):
            # 如果type参数为空,返回元素是否为选中状态,True/False (默认)
            bools = element.is_selected()
            if bools:
                return True
            else:
                return False
    
        # 退出所有浏览器
        def quit(self):
            self.driver.quit()
            # 清除chromedriver进程
            # os.system('taskkill /im chromedriver.exe /F')
    
        # 关闭当前浏览器
        def close(self):
            self.driver.close()
    
        # 移除元素的readonly属性,如:根据日期查询
        def remove_readonly(self, element):
            self.driver.execute_script('arguments[0].removeAttribute("readonly")', element)
    
        # 采用js方式,为元素设置value值
        def set_value_js(self, value, element):
            scripts = "arguments[0].value = '{}'".format(value)
            self.driver.execute_script(scripts, element)
    
        # 切换新的窗口 num为索引值 0为之前标签页,1为切换之后标签页
        def switch_to_window(self, num):
            # windows 为list
            windows = self.driver.window_handles
            # 若windows的长度为1,则新标签页打开失败
            if len(windows) == 1:
                return False
            else:
                try:
                    self.driver.switch_to_window(windows[num])
                except IndexError as e:
                    raise e
    
        # 获取当前标签页的 url
        @property
        def get_current_url(self):
            return self.driver.current_url
    不积跬步,无以至千里;不积小流,无以成江海。
  • 相关阅读:
    PHP中new static()与new self()的比较
    【程序员感悟系列】 由一点业务说开去
    配置管理工具 Puppet的安装和使用
    【读书笔记】大话设计模式
    博客还是要写起来 2016.08.13 周六
    linux启动SSH及开机自动启动
    Monkey日志信息的11种Event percentages
    Monkey官方帮助翻译&介绍
    Github问题An error occurred trying to download
    怎样克服拖延症,马上采取行动?
  • 原文地址:https://www.cnblogs.com/xuezhimin-esage-2020/p/14484791.html
Copyright © 2011-2022 走看看