判断元素是否存在

01使用name的方式定位

执行代码

  • 通过name的方式
    1. @staticmethod
    2. def isElementPresent():
    3. # 判断页面元素是否存在
    4. operation = ActionModule(method=Vue2ElementMethod.get_element,handle_value="其他出入库详情-按钮栏-反审核",sleep_time=2)
    5. err, res = ea.run_case_for_module(operation)
    6. assert err is None, err
    7. if res:
    8. return True
    9. else:
    10. # 找不到元素时输出none
    11. return False
    @pytest.mark.skip(reson="异常类")
    @pytest.mark.dependency(depends=["open_page"])
    def test_isElementPresent():
      elem = otherInStock.isElementPresent()
      if elem:
          print("所查找的元素存在于页面上")
      else:
          print("所查找的元素不存在于页面上")
    

    02使用异常类的方式

    执行代码

    def isElementPre(self,by,value):
      # from selenium.common.exceptions import NoSuchElementException下载依赖
      try:
          element = self.driver.find_element(by = by,value= value)
      except NoSuchElementException as e:
          print(e)
          # 发生了NoSuchElementException异常,说明页面未找到该元素,返回False
          return False
      else:
          # 没有发生异常,表示在页面找到元素
          return True
    
    @pytest.mark.skip(reson="异常类")
    def test_isElementPresent(self):
      # 判断元素id属性值为query的页面元素是否存在
      elem = otherInStock.isElementPresent("id","query")
      if elem:
          print("所查找的元素存在于页面上")
      else:
          print("所查找的元素不存在于页面上")
    

    判断元素是否可用