How do I verify that an element does not exist in Selenium 2

前端 未结 8 1095
误落风尘
误落风尘 2020-12-05 05:29

In Selenium 2 I want to ensure that an element on the page that the driver has loaded does not exist. I\'m including my naive implementation here.

    WebEle         


        
8条回答
  •  时光说笑
    2020-12-05 05:43

    If you are testing using junit and that is the only thing you are testing you could make the test expect an exception using

    @Test (expected=NoSuchElementException.class)
    public void someTest() {
        driver.findElement(By.className("commentEdit"));
    }
    

    Or you could use the findElements method that returns an list of elements or an empty list if none are found (does not throw NoSuchElementException):

    ...
    List deleteLinks = driver.findElements(By.className("commentEdit"));
    assertTrue(deleteLinks.isEmpty());
    ...
    

    or

    ....
    assertTrue(driver.findElements(By.className("commentEdit")).isEmpty());
    ....
    

提交回复
热议问题