WebDriver API二次封装脚本

''''

@Project: xxx
@Description: webdriver API二次封装类
@Time:2019/11/11 18:51
@Author:MING

'''

from selenium import webdriver
from selenium.webdriver.support.select import Select

# 创建类
class AutoDriver():

    # 构造初始化方法
    def __init__(self):
        # 浏览器对象实例化
        self.driver = webdriver.Firefox()
        # 绑定URL
        self.base_url ='http://www.baidu.com/'

    #打开URL
    def open_url(self,url):
        self.driver.get(self.base_url+url)

    #全屏
    def max_window(self):
        self.driver.maximize_window()

    #关闭浏览器 close quit
    def close_page(self):
        self.driver.close()

    #八大定位
    def find_e_element(self,by,selector):
        if by=='id' or by =='i':
            return self.driver.find_element_by_id(selector)
        elif by=='name' or by=='n':
            return self.driver.find_element_by_name(selector)
        elif by=='class_name' or by=='c':
            return self.driver.find_element_by_class_name(selector)
        elif by=='tag_name' or by=='t':
            return self.driver.find_element_by_tag_name(selector)
        elif by=='link_text' or by=='l':
            return self.driver.find_element_by_link_text(selector)
        elif by=='partial_link_text' or by=='plt':
            return self.driver.find_element_by_partial_link_text(selector)
        elif by=='xpath' or by=='x':
            return self.driver.find_element_by_xpath(selector)
        elif by=='css_selector' or by=='cs':
            return self.driver.find_element_by_css_selector(selector)

    #警告框
    def switch_alert(self,value):
        if value=='accept' or value=='a':
            return self.driver.switch_to.alert.accept()
        elif value=='dismiss' or value=='d':
            return self.driver.switch_to.alert.dismiss()

    #下拉 by:属性  selector:属性值  val:下拉属性  value:下拉值
    def select_value(self,by,selector,val,value):
        if val=='value' or val=='v':
            return Select(self.find_e_element(by,selector)).select_by_value(value)
        elif val=='index' or val=='i':
            return Select(self.find_e_element(by,selector)).select_by_index(value)
        elif val=='visible_text' or val=='vt':
            return Select(self.find_e_element(by,selector)).select_by_visible_text(value)

 

你可能感兴趣的:(WebDriver API二次封装脚本)