单击元素,直到使用isDisplayed

时间:2015-07-14 13:13:09

标签: javascript angularjs selenium-webdriver promise protractor

我想点击后退按钮,直到我到达我的应用中的特定页面(我expectedElement所在的位置)。这似乎不起作用......

失败,因为找不到预期的元素。

function navigateBack() {
    var backButton = $('div.back');
    var expectedElement = $('span.some-specific-class');

    backButton.click().then(function () {
        if (expectedElement.isDisplayed()) {
            expectedElement.click();
        } else {
            navigateBack();
        }
    });
}

我做了一些修改,以便在isDisplayed()上使用承诺。在缺少元素上调用isDisplayed()时,量角器会抛出错误...

var expectedElement = $('span.some-specific-class');

expectedElement.isDisplayed().then(function(isDisplayed) {
    if(isDisplayed)
        console.log('isDisplayed');
});

所以,除非这是无效的语法,如果量角器会在调用isPresent() / isDisplayed()时抛出错误,如何检查元素是否存在?!

1 个答案:

答案 0 :(得分:1)

isDisplayed()抛出的

Handle the error

function navigateBack() {
    var backButton = $('div.back');
    var expectedElement = $('span.some-specific-class');

    backButton.click().then(function () {
        expectedElement.isDisplayed().then(function (isDisplayed) {
            if (isDisplayed) {
                expectedElement.click();
            } else {
                navigateBack();
            }
        }, function (err) {

        });
    });
}