---
title: "how to verify an element does NOT exist in Selenium 2"
author: "eve"
date: "2011-10-26T11:22:49Z"
tags: []
categories: ["Web前端"]
slug: "how-to-verify-an-element-does-not-exist-in-selenium-2"
draft: false
lastmod: "2011-10-26T11:22:49Z"
id: 1216136
url: "https://eve.iteye.com/blog/1216136"
---

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