Edit: Not sure why this was marked as answered as neither of the questions linked deal with this issue, nor use Javascript. I am looking for a way to catch this error, not prevent it as methods like invisibilityOfElementLocated are unavailable in the Javascript version.
I have a website that I'm setting up some basic automated tests on using nodeJS, selenium and mocha. When first opening the site users click a box and dismiss a welcome message (here 'elemId') which then is no longer rendered in the DOM. I need to check it no longer exists before beginning the actual tests but despite using stalenessOf I frequently get a selenium error of NoSuchElementError. This wouldn't be a problem if I could catch and ignore this exception but unfortunately catching it doesn't seem to work. Below is the code in question and the error message I receive.
// Check it no longer exists before moving on
driver.wait(until.stalenessOf(driver.findElement(By.id('elemId'))))
.then(null, function(err) {
// Sometimes this throws an error even though the element is stale
// Using this syntax should catch the error, as it is a selenium error, not a javascript error
console.log('Threw error but that is acceptable');
})
NoSuchElementError: no such element: Unable to locate element: {"method":"css selector","selector":"*[id="elemId"]"}