Assert that a WebElement is not present using Selenium WebDriver with java Assert that a WebElement is not present using Selenium WebDriver with java java java

Assert that a WebElement is not present using Selenium WebDriver with java


It's easier to do this:

driver.findElements(By.linkText("myLinkText")).size() < 1


I think that you can just catch org.openqa.selenium.NoSuchElementException that will be thrown by driver.findElement if there's no such element:

import org.openqa.selenium.NoSuchElementException;....public static void assertLinkNotPresent(WebDriver driver, String text) {    try {        driver.findElement(By.linkText(text));        fail("Link with text <" + text + "> is present");    } catch (NoSuchElementException ex) {         /* do nothing, link is not present, assert is passed */     }}


Not Sure which version of selenium you are referring to, however some commands in selenium * can now do this:http://release.seleniumhq.org/selenium-core/0.8.0/reference.html

  • assertNotSomethingSelected
  • assertTextNotPresent

Etc..