0

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"]"}

jcreek
  • 43
  • 10

1 Answers1

0

The Exception occur before calling stalenessOf.
The code

driver.findElement(By.id('elemId')

will be generating the exception NoSuchElementFound.

Method stalenessOf:
The method stalenessOf is not doing what you think it does. The method expect the element to be present and will wait until a StaleElementException is being raised. Meaning that the element exist but then get either refresh or removed from the DOM.

Try with using Method invisibilityOfElementLocated:

 wait.until(ExpectedConditions.invisibilityOfElementLocated(By.id("elemId"));

The method will return True for either of those conditions:

  1. Element was never attached to the DOM
  2. Once the element is not present on the DOM in your set time. If still present it will raise a TimeOutException.
Nic Laforge
  • 1,776
  • 1
  • 8
  • 14