from time import sleep
from selenium import webdriver
from selenium.common.exceptions import TimeoutException
from selenium.webdriver import ActionChains
from selenium.webdriver.ie.options import Options
from selenium.webdriver.remote.webdriver import WebDriver
from selenium.webdriver.remote.webelement import WebElement
from selenium.webdriver.support import expected_conditions
from selenium.webdriver.support.wait import WebDriverWait
import sys
# BasePage的定义是,它是一个其他page的公共方法的封装,它是一个底层使用的框架
class BasePage:
def __init__(self, driver_base_page: WebDriver = None):
if driver_base_page is None:
_driver_type = 'chrome'
if _driver_type == 'chrome':
self.driver = webdriver.Chrome()
elif _driver_type == 'ie':
cap = Options()
cap.ignore_zoom_level = True
self.driver = webdriver.Ie(options=cap)
else:
self.driver = driver_base_page
# if self._base_url != '':
# self.driver.get(self._base_url)
# # 窗口最大化
self.driver.maximize_window()
# 添加隐式等待,判断元素是否加载到DOM
self.driver.implicitly_wait(10)
# 封装查找单个元素+显示等待6秒
def _find(self, *locator) -> WebElement:
# 优先先判断页面有没有加载等待的标识
js1 = 'return document.getElementsByClassName("ant-spin-dot")'
# 获取查询等待的标识
ele = len(self.driver.execute_script(js1))
while ele > 0: # 查询到等待标识则继续查询标识,直到标识消失
ele = len(self.driver.execute_script(js1))
WebDriverWait(self.driver, 5).until(lambda x: x.find_element(*locator), message="没有找到元素")
return self.driver.find_element(*locator)
# 封装多个元素查找方法
def _finds(self, *locator) -> list[WebElement]:
# 优先先判断页面有没有加载等待的标识
js1 = 'return document.getElementsByClassName("ant-spin-dot")'
# 获取查询等待的标识
ele = len(self.driver.execute_script(js1))
while ele > 0: # 查询到等待标识则继续查询标识,直到标识消失
ele = len(self.driver.execute_script(js1))
WebDriverWait(self.driver, 5).until(lambda x: x.find_element(*locator), message="没有找到元素")
return self.driver.find_elements(*locator)
# 查找元素后进行点击操作,仅用于鼠标左击
def _click(self, *locator):
# 优先先判断页面有没有加载等待的标识
js1 = 'return document.getElementsByClassName("ant-spin-dot")'
# 获取查询等待的标识
ele = len(self.driver.execute_script(js1))
while ele > 0: # 查询到等待标识则继续查询标识,直到标识消失
ele = len(self.driver.execute_script(js1))
WebDriverWait(self.driver, 5).until(lambda x: x.find_element(*locator), message="没有找到元素")
try:
WebDriverWait(self.driver, 3).until(
expected_conditions.element_to_be_clickable(locator))
self.driver.find_element(*locator).click()
except TimeoutException:
self.driver.find_element(*locator).click()
def _select(self, *locator):
WebDriverWait(self.driver, 5).until(lambda x: x.find_element(*locator), message="没有找到元素")
element = self.driver.find_element(*locator)
sleep(0.5)
return element
# 获取查询结果
def _get_search_res(self, *locator):
js1 = 'return document.getElementsByClassName("ant-spin-dot")'
# 优先判断页面是否存在等待的标识
ele = len(self.driver.execute_script(js1))
while ele > 0: # 查询到等待标识则继续查询标识,直到标识消失
ele = len(self.driver.execute_script(js1))
else:
# 因为查询结束后页面更新数据需要时间,强制等待1.5秒
sleep(1.5)
try:
WebDriverWait(self.driver, 8).until(lambda x: x.find_element(*locator), message="没有找到元素")
elements = self.driver.find_elements(*locator)
except TimeoutException:
elements = None
return elements
# 等待页面加载完成
def _wait(self, time=0.5):
js1 = 'return document.getElementsByClassName("ant-spin-dot")'
# 获取查询等待的标识
ele = len(self.driver.execute_script(js1))
while ele > 0: # 查询到等待标识则继续查询标识,直到标识消失
ele = len(self.driver.execute_script(js1))
sleep(time)
# 搜索后勾选一条数据(find)
def _find_to_search_one(self, *locator):
# 查询过程,等待查询动作完成才能去获取查询列表,所以对页面查询的是否存在查询等待的元素
js1 = 'return document.getElementsByClassName("ant-spin-dot")'
ele = len(self.driver.execute_script(js1))
# 轮询查询等待的元素是否存在
while ele > 0:
ele = len(self.driver.execute_script(js1))
else: # 元素不存在是说明查询结束了
WebDriverWait(self.driver, 60, 0.5).until(
expected_conditions.presence_of_element_located(locator))
sleep(1)
element = self.driver.find_element(*locator)
return element
# 鼠标左键双击
def _double_ckick(self, *locator):
# 获取目标元素
ele_input = self.driver.find_element(*locator)
ActionChains(self.driver).double_click(ele_input).perform() # 使用双击目标元素
sleep(1)
# 封装关闭浏览器,资源回收
def quit(self):
return self.driver.quit()
if __name__ == "__main__":
# print(sys.path)
sys.path.append("E:\git\auto-test\fusion_fintrade\pages\login.py")
print(sys.path)