Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Check if an element exists in Protractor

I have been trying to use the code snippet below to check if the element I'm looking for exists, however all I get is "Failed: No element found using locator: By(css selector, .icon-cancel)". What I want the program to do is to execute b()

element(by.css('.icon-cancel')).isDisplayed().then(function(result) {
    if ( result ) {
        a();
    } else {
        b();
    }
});
like image 805
user3400351 Avatar asked Jun 09 '16 14:06

user3400351


1 Answers

isDisplayed() would fail if an element does not actually exist in the DOM tree. You need the isPresent() method instead:

$('.icon-cancel').isPresent().then(function(result) {
    if ( result ) {
        a();
    } else {
        b();
    }
});
like image 108
alecxe Avatar answered Sep 21 '22 03:09

alecxe