Member-only story

Nightwatch.js — How to check if an element exists without creating an exception

Marika Lam
Feb 3, 2022

--

Method 1: Check with the Element method

You can achieve this by using the Selenium protocol “element” and a callback function to check the result status to determine if the element was found. For example:

browser.element('css selector', '#advanced-search', function(result){
if(result.status != -1){
//Element exists, do something
} else{
//Element does not exist, do something else
}
});

Method 2: Check with the isVisible method

From the nightwatch documentation, we see that in the callback you can check the result.value property to see whether or not the element was visible, i.e:

browser.isVisible('#advanced-search', results => {
if (results.value) { /* is visible */ }
else { /* is not visible */ }
});

--

--

Responses (1)