繁体   English   中英

如何使用 Python 和 Appium 检查元素不存在?

[英]How do I check that an element does not exist, using Python and Appium?

请查看我正在使用的代码片段。

下面是我的步骤定义文件, home_step.py

@Then('the screen is verified')
def step_impl(context):
   context.app.home_page.check_element_nonexistent('Some Tab')

下面是我的页面对象文件, home_page.py

#Element's accessibility-id is defined here
def __init__(self, driver):
   super().__init__(driver)
   self.driver = driver
   self.some_tab = 'some-id'

#I call find_element() here
def el_some_tab(self):
   return self.driver.find_element(AppiumBy.ACCESSIBILITY_ID, self.some_tab)

#Attempt to check for nonexistent elements
def check_element_nonexistent(self, element)
   try:
      if element == 'Some Tab':
         assert self.el_some_tab().is_displayed()
   except NoSuchElementException:
      return False
   return True

我尝试检查不存在的项目来自这个答案

上面的代码片段运行成功,它不应该因为元素, some-tab在运行期间不存在。

  def is_displayed(self, locator: Locator) -> bool:
        is_displayed: bool = False
        count = len(self.driver.find_elements(*locator))
        if count > 0:
            is_displayed = True
        return is_displayed

或者

    # try:
    #     is_displayed = self.driver.find_element(*locator).is_displayed()
    # except NoSuchElementException:
    #     l.info(f'{locator} is displayed: {is_displayed}')

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM