how to verify an element does NOT exist in Selenium 2

WebElement element = driver.findElements(By.id("block-id"));

assertNull(element);

    this code snippet will get a NoSuchEleementException, use the following 2 implementations instead

 

1. make the test expect the very exception

 

    @Test(expected = org.openqa.selenium.NoSuchElementException.class)
    public void haveNoRecommendation() {
        ...
        driver.findElement(By.id("block-id"));
    }
 

 

 

2. use findElements instead of findElement

 

    public void elementNotExist(String id) {
        List<WebElement> elements = driver.findElements(By.id(id));
        assertTrue(elements.isEmpty());
    }

 

 

你可能感兴趣的:(Selenium 2)