Search code examples
javaseleniumselenium-webdriverwebdrivernosuchelementexception

Assert Absence of element in the web page, giving NoSuchElementException


Need to assert that there is no such element in the webpage, When tried with fieldValueBox.isDisplayed(); instead of "false" its throwing "NoSuchElementFound" exception. Right now i was using 'try catch' and making decision in 'catch'


Solution

  • If the element is not on the page, then you will get 'NoSuchElementFound' exception. You can try checking that the number of elements with the locator is zero:

    private boolean elementNotOnPage(){
        boolean elementIsNotOnPage = false;
    
        List<WebElement> element = driver.findElements(yourLocator);
    
        if(element.size() == 0){
            elementIsNotOnPage = true;
        }
    
        return elementIsNotOnPage;
    
    }