当前位置: 代码迷 >> Web前端 >> how to verify an element does NOT exist in Selenium 二
  详细解决方案

how to verify an element does NOT exist in Selenium 二

热度:1056   发布时间:2012-08-25 10:06:20.0
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());
    }

?

?

  相关解决方案