How do I verify that an element does not exist in Selenium 2

0
0

In Selenium 2 I want to ensure that an element on the page that the driver has loaded does not exist. I’m including my naive implementation here.

    WebElement deleteLink = null;
try {
deleteLink = driver.findElement(By.className("commentEdit"));
} catch (NoSuchElementException e) {
}
assertTrue(deleteLink != null);

Is there a more elegant way that basically verifies to assert that NoSuchElementException was thrown?

  • You must to post comments
0
0

If you are testing using junit and that is the only thing you are testing you could make the test expect an exception using

@Test (expected=NoSuchElementException.class)
public void someTest() {
driver.findElement(By.className("commentEdit"));
}

Or you could use the findElements method that returns an list of elements or an empty list if none are found (does not throw NoSuchElementException):

...
List<WebElement> deleteLinks = driver.findElements(By.className("commentEdit"));
assertTrue(deleteLinks.isEmpty());
...

or

....
assertTrue(driver.findElements(By.className("commentEdit")).isEmpty());
....
  • You must to post comments
Showing 1 result
Your Answer
Post as a guest by filling out the fields below or if you already have an account.
Name*
E-mail*
Website