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.

WebDriverManager.chromedriver().setup();
        ChromeDriver driver = new ChromeDriver();

        driver.get("https://www.programsbuzz.com/search/node?keys=selenium");

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

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

        driver.close();
  • 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.