Webdriver how to verify element is not displayed in Java.

This code snippet can be used when there is a hard coded element.

// WebElement first search result link can be captured using search_results_scroll.
boolean isDiplayedKnowledgeBase=false;
WebElement firstSearchResult = search_results_scroll.findElement(By.("ul > li"));
catch(NoSuchElementException e){
System..println("Unwanted element is not displayed.");
Assert.(false, isDiplayedKnowledgeBase,"This facility is listed for unwanted users.");

If the element is passed form page object model can use below code snippet.

Pass the element from the page Object

(gatewayLandingPage.lbl_WhatNeedTohappenWithThis);public void elementNotPresentVerification(WebElement element) {
try {
Assert.(false, element.isDisplayed(), "Unwanted element is displayed " + element);
} catch (NoSuchElementException e) {
System..println("Unwanted element is not found");



Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store