Aug 10, 2022
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;
WebCommand.pause(6000);
try{
WebElement firstSearchResult = search_results_scroll.findElement(By.cssSelector("ul > li"));
isDiplayedKnowledgeBase=firstSearchResult.isDisplayed();
}
catch(NoSuchElementException e){
System.out.println("Unwanted element is not displayed.");
}
Assert.assertEquals(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
elementNotPresentVerification(gatewayLandingPage.lbl_WhatNeedTohappenWithThis);public void elementNotPresentVerification(WebElement element) {
try {
Assert.assertEquals(false, element.isDisplayed(), "Unwanted element is displayed " + element);
} catch (NoSuchElementException e) {
System.out.println("Unwanted element is not found");
}
}