简体   繁体   English

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

[英]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下面是我的步骤定义文件, 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下面是我的页面对象文件, 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.上面的代码片段运行成功,它不应该因为元素, 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

OR或者

    # 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.

相关问题 我如何让python接受元素0和1(如果存在)而取元素0(如果元素1不存在)呢? - How do I get python to take element 0 and 1 if they exist and take element 0 if element 1 does not exist? 如何使用python检查文本文件中是否存在数据? - How do I check a data exist or not in text file using python? 如何使用python和appium滚动到未出现在视图中的元素 - How to scroll to an element that does not appear in the view using python and appium 使用Appium / Python,如何检查键盘是否显示? - Using Appium / Python, how can I check if the Keyboard is showing or not? 如何检查元素的一部分是否存在于python中的数组中? - How to check if part of an element exist in array in python? Python - 如何检查页面中不应该存在的元素 - Python - How to check a element that should not exist in the page 如何检查元素是否在元素内? | Selenium | Python - How do I check If an element is inside an element? | Selenium | Python 使用 pandas,如何检查列中是否存在特定序列? - Using pandas, how do I check if a particular sequence exist in a column? 用于检查元素是否存在的Python Selenium Webdriver需要时间 - Python Selenium Webdriver to check if element does NOT exist takes time 如何检查数据中是否存在键,如果存在则执行for循环,否则在python中使用列表理解忽略 - How to check if key exist in data, if exist then do for loop else ignore using list comprehension in python
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM