`
eve
  • 浏览: 13114 次
  • 性别: Icon_minigender_1
  • 来自: 杭州
最近访客 更多访客>>
社区版块
存档分类
最新评论

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

 

 

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics