webdriver封装

安木 0
# -*- coding: utf-8 -*-
"""
@Author:5PING
@IDE:PyCharm
@File:browser.py
@Datetime:2022/6/25 1:01
"""
import os
import time

from selenium import webdriver
from selenium.webdriver import ActionChains, Keys
from selenium.webdriver.chrome.service import Service
from selenium.webdriver.common.by import By
from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC


class Browser():
    def __init__(self):
        current_path = os.path.dirname(os.path.abspath(__file__))
        driver_path = current_path + '/../webdriver/chromedriver.exe'
        driver_service = Service(driver_path)
        driver = webdriver.Chrome(service=driver_service)  # 实例化浏览器的对象
        driver.maximize_window()
        self.driver = driver
    # 定位方式
    def css(self, loc):
        return self.driver.find_element(By.CSS_SELECTOR, loc)

    def xpath(self, loc):
        return self.driver.find_element(By.XPATH, loc)

    def link(self, loc):
        return self.driver.find_element(By.PARTIAL_LINK_TEXT, loc)

    def byid(self, loc):
        return self.driver.find_element(By.ID, loc)

    def name(self, loc):
        return self.driver.find_element(By.NAME, loc)

    def cname(self, loc):
        return self.driver.find_element(By.CLASS_NAME, loc)

    def tname(self, loc):
        return self.driver.find_element(By.TAG_NAME, loc)

    # 隐式等待
    def implicitly_wait(self, timeout):
        return self.driver.implicitly_wait(timeout)

    # 显式等待
    def element_wait(self, loc, timeout, poll_frequency=0.5):
        # WebDriverWait(self.driver, timeout,).until(EC.presence_of_element_located((By.ID, value)), messages)
        WebDriverWait(self.driver, timeout, poll_frequency) \
            .until(EC.visibility_of_element_located((By.CSS_SELECTOR, loc)))

    # 浏览器api
    def open(self, url):
        self.driver.get(url)
        self.driver.implicitly_wait(20)

    def close(self):
        self.driver.close()

    def quit(self):
        self.driver.quit()

    def refresh(self):
        self.driver.refresh()

    def back(self):
        self.driver.back()

    def forward(self):
        self.forward()

    # 浏览器api-窗口
    def max_window(self):
        self.driver.maximize_window()

    def min_window(self):
        self.driver.minimize_window()

    def set_window(self, width, height):
        self.set_window(width, height)

    # 浏览器api-属性
    def get_title(self):
        title = self.driver.title
        return title

    def get_url(self):
        url = self.driver.current_url
        return url

    def get_source(self):
        page_source = self.driver.page_source
        return page_source

    def take_screenshot(self, path):
        try:
            self.driver.get_screenshot_as_file(path)
        except Exception:
            print("Unable to get the current window screenshot,path: {1}".format(path))

    # 分离定位方式和定位
    def depart_by_loc(self, byloc):
        by = byloc.split('->')[0]
        loc = byloc.split('->')[1]
        if by == 'css':
            return self.css(loc)
        elif by == 'xpath':
            return self.xpath(loc)
        elif by == 'link':
            return self.link(loc)
        elif by == 'id':
            return self.byid(loc)
        elif by == 'name':
            return self.name(loc)
        elif by == 'cname':
            return self.cname(loc)
        elif by == 'tname':
            return self.tname(loc)
        else:
            raise NameError("Please enter the correct targeting elements,"
                            "'id','name','cname','tname','link','xpath','css'.")

    # 元素操作api-获取属性
    def get_attribute(self, byloc, attribute):
        el = self.depart_by_loc(byloc)
        attr = el.get_attribute(attribute)
        return attr

    def get_text(self, byloc):
        el = self.depart_by_loc(byloc)
        text = el.text
        return text

    # 元素操作api-操作
    def send_keys(self, byloc, text):
        el = self.depart_by_loc(byloc)
        el.send_keys(text)

    def click(self, byloc):
        el = self.depart_by_loc(byloc)
        el.click()

    def clear(self, byloc):
        el = self.depart_by_loc(byloc)
        el.clear()

    def submit(self, byloc):
        el = self.depart_by_loc(byloc)
        el.submit()
    # 鼠标操作-单击双击右键移动
    def mouse_click(self,byloc):
        mouse = ActionChains(self.driver)
        el = self.depart_by_loc(byloc)
        mouse.click(el).perform()

    def mouse_double_click(self, byloc):
        mouse = ActionChains(self.driver)
        el = self.depart_by_loc(byloc)
        mouse.double_click(el).pause(1).perform()

    def context_click(self,byloc):
        mouse = ActionChains(self.driver)
        el = self.depart_by_loc(byloc)
        mouse.context_click(el).perform()
    def move_to_element(self,byloc):
        mouse = ActionChains(self.driver)
        el = self.depart_by_loc(byloc)
        mouse.move_to_element(el).pause(1).perform()
    # 键盘操作-ctrl alt shift enter tab
    # 弹框-确定取消输入取文本
    def alert_text(self):
        return self.driver.switch_to.alert.text
    def alert_accept(self):
        self.driver.switch_to.alert.accept()
    def alert_dismiss(self):
        self.driver.switch_to.alert.dismiss()
    def alert_send_keys(self,wd):
        self.driver.switch_to.alert.send_keys(wd)
    # 跳转框架-跳进跳出
    def switch_in_frame(self,frame):
        self.driver.switch_to.frame(frame)
    def switch_out_frame(self):
        self.driver.switch_to.default_content()
    # 跳转窗口-两~三种
    def switch_handle(self):
        handles = self.driver.window_handles
        self.driver.switch_to.window(handles[-1])
    # 输入+enter
    def sendkeys_enter(self,byloc,wd):
        el = self.depart_by_loc(byloc)
        el.send_keys(wd)
        el.send_keys(Keys.ENTER)

    # 调用js
    def js(self, script):
        self.driver.execute_script(script)
    # js滚动条-滚下滚上
    def scroll_bottom(self):
        self.driver.execute_script('window.scrollTo(0,document.body.scrollHeight);')
    def scroll_top(self):
        self.driver.execute_script('window.scrollTo(0,0);')
    # js属性-移除修改高亮
    # ctrl+c+v
    def ctrl_c(self,byloc):
        keyboard = ActionChains(self.driver)
        el = self.depart_by_loc(byloc)
        keyboard.key_down(Keys.CONTROL).send_keys('c').key_up(Keys.CONTROL).perform()
    def ctrl_v(self,byloc):
        keyboard = ActionChains(self.driver)
        el = self.depart_by_loc(byloc)
        keyboard.key_down(Keys.CONTROL).send_keys('v').key_up(Keys.CONTROL).perform()
    # 文件上传
    def autoit_upload(self,byloc,script):
        el = self.depart_by_loc(byloc)
        kb = ActionChains(self.driver)
        kb.click(el).perform()
        time.sleep(3)
        os.system(script)
    def input_upload(self,byloc,upload):
        self.depart_by_loc(byloc).send_keys(upload)

if __name__ == '__main__':
    pass # 测试代码

 


发表回复