Verify Text is not Present in Selenium

Profile picture for user arilio666

We can verify a text not present on a webpage in selenium by using asserFalse assertion from either JUnit or TestNG.

        ChromeDriver driver = new ChromeDriver();


        WebElement bodyFull = driver.findElement(By.tagName("body"));
        String bodyText = bodyFull.getText();

        Assert.assertFalse(bodyText.contains("Spam Virus"), "Spam Message Text Is Not Present");

  • Now consider this scenario where the page of your site is spammed with some texts.
  • These unwanted texts are later cleaned and fixed, and as a tester, we need to verify the text is no longer present on that page.
  • Using asserFalse and providing the unwanted text verifies and confirms that the texts are not there anymore.
  • This is how we can verify text is not present in selenium java.