检查量角器中是否存在元素

时间:2021-03-22 20:11:09

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()

我一直在尝试使用下面的代码片段来检查我正在寻找的元素是否存在,但是我得到的是“失败:使用定位器找不到元素:By(css selector,.icon-cancel)”。我希望程序做的是执行b()

element(by.css('.icon-cancel')).isDisplayed().then(function(result) {
    if ( result ) {
        a();
    } else {
        b();
    }
});

2 个解决方案

#1


12  

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

如果元素实际上不存在于DOM树中,则isDisplayed()将失败。您需要使用isPresent()方法:

$('.icon-cancel').isPresent().then(function(result) {
    if ( result ) {
        a();
    } else {
        b();
    }
});

#2


0  

One possibility is, if the element is loaded dynamically, the element may not have been loaded by the time your test is running. So you can wait for a few seconds for the element to be available.

一种可能性是,如果元素是动态加载的,则在测试运行时可能尚未加载该元素。所以你可以等几秒钟让元素可用。

var EC = protractor.ExpectedConditions;
var yourElement = element(by.css('.icon-cancel'));
browser.wait(EC.presenceOf(yourElement), 5000);

#1


12  

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

如果元素实际上不存在于DOM树中,则isDisplayed()将失败。您需要使用isPresent()方法:

$('.icon-cancel').isPresent().then(function(result) {
    if ( result ) {
        a();
    } else {
        b();
    }
});

#2


0  

One possibility is, if the element is loaded dynamically, the element may not have been loaded by the time your test is running. So you can wait for a few seconds for the element to be available.

一种可能性是,如果元素是动态加载的,则在测试运行时可能尚未加载该元素。所以你可以等几秒钟让元素可用。

var EC = protractor.ExpectedConditions;
var yourElement = element(by.css('.icon-cancel'));
browser.wait(EC.presenceOf(yourElement), 5000);