Tech Me More

To quench our thirst of sharing knowledge about our day to day experience & solution to techincal problems we face in our projects.

Advertise with us !
Send us an email at diehardtechy@gmail.com

Wednesday, January 3, 2018

What is the best way to check element not present in webpage using selenium webdriver ?


Best Way of checking element not present in webpage using selenium webdriver ?


Often encountered a situation where you have to verify an element is not present in the web page. 

using 


try {

        driver.findElement(locatorKey);

        Assert.assertTrue(false);

    } catch (org.openqa.selenium.NoSuchElementException e) {

        Assert.assertTrue(true);

    }

 


is not a right approach. Because this is layman way of asserting absence of web element.

As per selenium documantaion, best way to check if element is not present is using findElements and then asseting on the size of the list returned. 

findElements does not throw any exception in case if an element is not present. Asserting on 0 length is more appropriate for absence of an element. 

Selenium official comment : 




No comments: