page_action.py
在该模块里定义一个类,假设叫做PageBase,负责获取界面相关操作
from selenium.common.exceptions import NoSuchElementExceptionfrom utils.logger import log# 页面对象基础操作类(页面基类)class PageBase:driver=None#定义类的成员变量(属性),这个变量在子类的构造方法中赋值# 定位一个元素,参数locator是元组def locate_element(self,locator):try:e=self.driver.find_element(*locator)log.info(f"元素定位成功:{locator}")return eexcept NoSuchElementException:log.error(f"元素定位失败:{locator}")return None# 定位多个元素,参数locator是元组def locate_elements(self,locator):list1=self.driver.find_elements(*locator)if len(list1)==0:log.error(f"没有找到任何元素:{locator}")else:log.info(f"找到{len(list1)}个目标元素:{locator}")return list1# 点击一个元素def click(self,locator):self.locate_element(locator).click()log.info(f"点击元素完毕:{locator}")# 清空一个元素里的数据def clear(self,locator):self.locate_element(locator).clear()log.info(f"清空元素完毕:{locator}")# 输入一个元素里的文本def input_text(self,locator,text):self.locate_element(locator).send_keys(text)log.info(f"输入数据完毕:{locator},输入内容是:{text}")# 清空后输入def clear_and_input_text(self,locator,text):self.clear(locator)self.input_text(locator,text)
