Protractor, with isDisplayed() I get NoSuchElementError: No element found using locator

In protractor 2.0, I am checking in a expect() if one element is displayed. I expect a false, but the weird thing is that I get following error:

NoSuchElementError: No element found using locator:“userForm”)

My code is:

describe('closeModal', function() {
    it('should close the alert that appears after registration.', function(){

I understand that I get that error because element is not longer on the page (is what I want to confirm), but shouldn’t I get a false and not a error?

isDisplayed() would check if an element is visible or not, but you need to check whether an element is present in DOM or not, use isElementPresent() or isPresent():


See also:

This error is part of WebDriver behavior. For such cases you should better use
isPresent or isElementPresent

If element visible do A if not visible do B, disregard exception if element not found:

    if (visible) {
        // do A when element visible
        // do B when element not visible 
}, function () {
    //suppress exception if element is not found on page

.isDisplayed() assumes the element is present (exists in the DOM)

so if you do


but the element is not present, then instead of graceful failed expectation, $('[ng-show=saving]').isDisplayed() will throw an error causing the rest of it block not executed

Read More:   Close pop up on back button


If you assume, the element you’re checking may not be present for any reason on the page, then go with a safe way below

*  element is Present and is Displayed
*  @param    {ElementFinder}      $element       Locator of element
*  @return   {boolean}
let isDisplayed = function ($element) {
  return (await $element.isPresent()) && (await $element.isDisplayed())

and use

expect(await isDisplayed( $('[ng-show=saving]') )).toBe(true);

The answers/resolutions are collected from stackoverflow, are licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0 .

Similar Posts