selenium,基类封装


#coding=utf-8
from selenium.webdriver.support.select import Select

from util.base_driver import Base_Driver


class Base_Page:

    def __init__(self,driver):
        self.driver=Base_Driver().get_driver1()


    def find_element(self,*args):
        try:
            return self.driver.find_element(*args) #定位一个元素
        except Exception as e:
            print(e)
            return False

    def find_elements(self,*args):
        return  self.driver.find_elements(*args) #定位一组元素

    def input(self,value,*args):
        ele=self.find_element(*args)
        if ele: #判断元素是否定位到
            ele.clear() #清空
            ele.send_keys(value)    #输入

    def click(self,*args):
        ele =self.find_element(args)
        if ele:
            ele.click() # 点击

    def get_title(self):    #获取页面标题
        return self.driver.title

    def get_url(self):  #获取当前页面url
        return self.driver.current_url

    def get_page(self): #获取页面源码
        return self.driver.page_source

    def save_img(self,path): #截图
        self.driver.save_screenshot(path)

    def alert_acc(self): #警告框中点击确定
        self.driver.switch_to.alert.accept()

    def alert_dismiss(self):    #警告框中点击确定
        self.driver.switch_to.alert.dismiss()

    def alert_input(self,value): #警告框中输入值
        self.driver.switch_to.send_keys(value)

    def select_option(self,value,*args): #下拉框中选择选项
        ele=self.find_element(*args)
        if ele:
            Select(ele).select_by_value(value)

    def execute_js(self,js):    #执行js代码
        self.driver.execute_script(js)

    def switch_frame(self,*args): #切换框架
        ele=self.find_element(*args)    #定位框架
        if ele:
            self.driver.switch_toframe(ele)

    def switch_default(self): #切换到最外层框架
        self.driver.switch_to.defaulf_content()