【问题标题】:Python - How to check a element that should not exist in the pagePython - 如何检查页面中不应该存在的元素
【发布时间】:2016-05-05 09:25:14
【问题描述】:

我使用 selenium webdriver,我如何检查该元素是否不应该出现在页面中并且我正在测试 python。任何人都可以提出任何解决此问题的方法。

非常感谢。

【问题讨论】:

    标签: python validation selenium webdriver assert


    【解决方案1】:

    您可以通过多种方式做到这一点。懒惰是这样的。

    # Import these at top of page
    import unittest
    try: assert '<div id="Waldo" class="waldo">Example</div>' not in driver.page_source
    except AssertionError, e: self.verificationErrors.append("Waldo incorrectly appeared in page source.")
    

    或者您可以导入预期条件并断言它返回 present_of_element_located 不是 True。请注意,true 是大写敏感的,presence_of_element_located 要么返回 True 要么返回 Not Null,因此 assertFalse 不是更简单的表达方式。

    # Import these at top of page
    import unittest
    from selenium import webdriver
    from selenium.webdriver.common.by import By
    from selenium.webdriver.support import expected_conditions as EC
    
    try: assert EC.presence_of_element_located( (By.XPATH, '//*[@id="Waldo"]') ) is not True
    except AssertionError, e: self.verificationErrors.append('presence_of_element_located returned True for Waldo')
    

    或者像 Raj 说的,你可以使用 find_elements 并断言有 0。

    import unittest
    
    waldos = driver.find_elements_by_class_name('waldo')
    try: self.assertEqual(len(waldos), 0)
    except AssertionError, e: self.verificationErrors.append('Found ' + str(len(waldos)) + ' Waldi.')
    

    您还可以断言将发生 NoSuchElementException。

    # Import these at top of page
    import unittest
    from selenium import webdriver
    from selenium.webdriver.common.by import By
    from selenium.common.exceptions import NoSuchElementException
    
    try: 
        with self.assertRaises(NoSuchElementException) as cm:
            driver.find_element(By.CSS_SELECTOR, 'div.waldo')
    except AssertionError as e:
        raise e
    

    【讨论】:

    • 这真是个糟糕的答案,太可怕了。
    【解决方案2】:

    是的,试试它的一个衬里,使用简单

    if(driver.findElements(By.xpath("yourXpath/your locator stratgey")).size() >0){
                // if size is greater then zero that means element
                // is present on the page
            }else if(!(driver.findElements(By.xpath("yourXpath/your locator stratgey")).size() >0)){
                // if size is smaller then zero that means
                // element is not present on the page
            }
    

    【讨论】:

      【解决方案3】:
      try: 
          driver.find_elements_by_xpath('//*[@class="should_not_exist"]')
          should_exist = False
      except:
          should_exist = True
      
      if not should_exist:
          // Do something
      

      【讨论】:

        【解决方案4】:

        您可以创建一个 IsElementPresent 方法,该方法将返回页面上是否存在元素。您可以在测试用例中调用此方法。

        public boolean IsElementPresent(String locator, String locatorvalue) 
        {
            try 
            {   
                if(locator.equalsIgnoreCase("id"))
                {
                    driver.findElement(By.id(locatorvalue));                
                }
                return true;
                }
            catch (NoSuchElementException e) 
            {
                return false;
            }
        }
        

        【讨论】:

        • 问题是用python标签发布的,答案是Java。
        • 我们不希望在python 标签上出现这种可怕的东西
        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2021-02-23
        • 2016-01-04
        • 1970-01-01
        • 2011-09-25
        • 1970-01-01
        • 2015-05-30
        • 1970-01-01
        相关资源
        最近更新 更多