selenium中 基本方法定义,封装后直接调用,太方便了

# 导入timem模块的sleep
from time import sleep
#导入selenium里的expected_conditions,selenium包需要自己导入可网上下载
from selenium.webdriver.support import expected_conditions as ec
from selenium.webdriver.support.wait import WebDriverWait
#调用封装好的 获取时间戳的方法,自行百度
from SYS_Operation import sys_operation

class basefunction:

    # 定义方法实现对元素的点击
    def click_element(self, ele):
        ele.click()

    # 定义方法实现给元素输入字符串
    def send_keys_to_element(self,ele, value):
        ele.clear()
        ele.send_keys(value)

    # 定义方法获取元素的某个属性值,并返回
    def get_attribute_from_element(self,ele, attributename):
        attribute_value = ele.get_attribute(attributename)
        return attribute_value

    #定义方法,实例化动态等待对象
    def get_webdriver_waiter(self, browser):
        waiter = WebDriverWait(browser, 5)
        return waiter

    # 定义方法,动态定位元素,定位到元素后,并返回
    def find_element_when_presence(self, browser, ele_tuple):
        waiter = self.get_webdriver_waiter(browser)
        ele = waiter.until(ec.presence_of_element_located(ele_tuple))
        return ele

    # 定义方法,实现动态点击某个元素
    def find_element_and_click_when_presence(self,browser, ele_tuple):
        ele = self.find_element_when_presence(browser, ele_tuple)  # 定位元素
        self.click_element(ele)  # 点击元素

    #定义方法,实现动态定位元素并输入内容
    def find_element_and_send_keys_when_presence(self,browser, ele_tuple, value):
        ele = self.find_element_when_presence(browser, ele_tuple)  # 定位元素
        self.send_keys_to_element(ele,value)  #给元素传值

    #定义方法,实现动态定位元素并获取其某个属性值
    def find_element_and_get_attribute_when_presence(self, browser, ele_tuple, attributename):
        ele = self.find_element_when_presence(browser, ele_tuple)
        attribute_value = self.get_attribute_from_element(ele,attributename)
        return attribute_value

    # 定义方法,实现动态定位元素并获取元素上的文本
    def find_element_and_get_text_when_presence(self, browser, ele_tuple):
        ele = self.find_element_when_presence(browser, ele_tuple)
        return ele.text

    # 定义方法,实现动态多元素定位,当定位到元素返回一个包含元素的列表,如果定位不到则返回空列表
    def find_elements_when_presence(self, browser, ele_tuple):
        try:
            waiter = self.get_webdriver_waiter(browser)  # 调用get_webdriver_waiter获取动态等待对象
            ele_list = waiter.until(ec.presence_of_all_elements_located(ele_tuple))
            return ele_list
        except:
            return []

    #定义方法,实现动态定位元素,并按照元素索引点击
    def find_elements_and_click_by_index_when_presence(self, browser, ele_tuple, index):
        ele_list = self.find_elements_when_presence(browser, ele_tuple)
        self.click_element(ele_list[index])

    #定义方法,实现动态判断某个元素是否再页面上,在则返回True,不在则返回False
    def element_present_in_web_or_not(self, browser, ele_tuple):
        try:
            waiter = self.get_webdriver_waiter(browser)  # 调用get_webdriver_waiter获取动态等待对象
            waiter.until(ec.presence_of_element_located(ele_tuple))
            return True
        except:
            return False

    # 定义方法,实现截图,并图片存放到指定目录下
    def get_web_picture(self,browser, picture_path):
        # 组织路径+图片名称+格式为一个字符串
        picture =picture_path + "\\"+sys_operation().get_timestr() + ".png"
        sleep(2)
        browser.switch_to.default_content()  # 切出框架
        browser.get_screenshot_as_file(picture)

    # 定义方法,实现动态切入框架
    def switch_to_frmame_by_webdriverwait(self, browser, ele_tuple):
        waiter = self.get_webdriver_waiter(browser)
        waiter.until(ec.frame_to_be_available_and_switch_to_it(ele_tuple))

    #定义一个方法switch_to_web_by_index(browser, index),实现按照索引切换网页。
    def switch_to_web_by_index(self, browser, index):
        handles = browser.window_handles
        browser.switch_to.window(handles[index])

    # 定义一个放法title_is_some_text_or_not(broser, str), 动态等待方式实现判断网页标题是否
    # 等于传入的str,相等则返回True,不相等返回False。
    def title_is_some_text_or_not(self, browser, str):
        try:
            waiter = self.get_webdriver_waiter(browser)
            r = waiter.until(ec.title_is(str))
            return r
        except:
            return False

你可能感兴趣的:(python,selenium,pycharm)