ITeye博客

how to verify an element does NOT exist in Selenium 2

eve · 2011-10-26 11:22 · selenium 2 · Web前端 · Markdown

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());
    }

评论(0)

暂无评论。

评论功能维护中,当前仅展示历史评论。