user603007
user603007

Reputation: 11794

how can I check whether an element does exist or not?

Starting to learn more about protractor. Question: how do I check whether an element exists on a page? The first property firstNsame does not exist. I would like to move on with the rest of the script if an element does not exist. I am using this script but looks like the isPresent() code does not work:

var data = {"firstNsame": "Joke", "lastName": "Moker", "stateId": 49};

for (var key in data) {
    try {
        if (data.hasOwnProperty(key)) {
            var el = element(by.model(key));
            if (el.isPresent())
            {
                console.log('element present',key);
            }
            else
            {
                console.log('element not present',key);
            }

        }
    }
    catch (err) {
        console.log('error occured',err);
    }
}

Upvotes: 1

Views: 153

Answers (1)

Andres D
Andres D

Reputation: 8900

Try the following:

element(by.model(key)).isPresent().then(function(present) {
  console.log('Element present', present)
});

Upvotes: 1

Related Questions