Verify Text is not Present in Selenium

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.
Thu, 03/16/2023 - 12:32
Ashwin possesses over five years of experience in the Quality Assurance industry and is currently serving as a Technical Lead at iVagus. His expertise encompasses a broad range of technologies, including Cypress, Rest Assured, Selenium, Cucumber, JavaScript and TypeScript.