简体   繁体   中英

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

Please see a snippet of what I am working with.

Below is my step definition file, home_step.py

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

Below is my page objects file, 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

My attempt to check for the nonexistent item came from this answer .

The above snippet runs successfully, it should not since the element, some-tab was nonexistent during the run.

  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

OR

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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