page_action.py

在该模块里定义一个类,假设叫做PageBase,负责获取界面相关操作

  1. from selenium.common.exceptions import NoSuchElementException
  2. from utils.logger import log
  3. # 页面对象基础操作类(页面基类)
  4. class PageBase:
  5. driver=None#定义类的成员变量(属性),这个变量在子类的构造方法中赋值
  6. # 定位一个元素,参数locator是元组
  7. def locate_element(self,locator):
  8. try:
  9. e=self.driver.find_element(*locator)
  10. log.info(f"元素定位成功:{locator}")
  11. return e
  12. except NoSuchElementException:
  13. log.error(f"元素定位失败:{locator}")
  14. return None
  15. # 定位多个元素,参数locator是元组
  16. def locate_elements(self,locator):
  17. list1=self.driver.find_elements(*locator)
  18. if len(list1)==0:
  19. log.error(f"没有找到任何元素:{locator}")
  20. else:
  21. log.info(f"找到{len(list1)}个目标元素:{locator}")
  22. return list1
  23. # 点击一个元素
  24. def click(self,locator):
  25. self.locate_element(locator).click()
  26. log.info(f"点击元素完毕:{locator}")
  27. # 清空一个元素里的数据
  28. def clear(self,locator):
  29. self.locate_element(locator).clear()
  30. log.info(f"清空元素完毕:{locator}")
  31. # 输入一个元素里的文本
  32. def input_text(self,locator,text):
  33. self.locate_element(locator).send_keys(text)
  34. log.info(f"输入数据完毕:{locator},输入内容是:{text}")
  35. # 清空后输入
  36. def clear_and_input_text(self,locator,text):
  37. self.clear(locator)
  38. self.input_text(locator,text)