Search

Tip | NullWebElement safe option for notFoundElement



The problem!


A common problem with selenium is when an element has not been found, and you try to interact with it, which causes an exception because the element is null.



The solution?


The simple solution is to use a method like this quick tip

This solution works but can be improved with a code that can safely invoke methods on the null webelement without threat a NullReferenceException, implementing an interface with methods that do nothing making the null webelement object predictable and safe.


Implementing the null WebElement


The implementation is quite easy, just have to extend the WebElement methods with safe ones.


Now, all we need to do is redefine the findBy methods to use this new NullWebElement.

With this implementation, we can verify if an element is or is not on the page without the null exception.


© 2020 Qa lemming