如果元素不存在,我在添加条件函数时得到的错误相同

时间:2017-09-15 17:37:36

标签: javascript protractor

目前我有一个页面,其中有一个随机显示的元素。为此,我创建了一个条件函数:

this.checkDropdownPresent = function (dropdownLocator, chooseOption) {
    dropdownLocator.isPresent().then(function(element) {
        if (element) {
            let select = dropdownLocator;
            select.element(by.cssContainingText('option', chooseOption)).click();
        }
    });
}; 

当元素显示在屏幕上时,该工作正常且量角器与它交互,但当屏幕中的元素未显示时,我收到消息:

Failed: element not visible: Element is not currently visible and may not be manipulated

有任何提示可以帮助我吗?

2 个答案:

答案 0 :(得分:1)

您还需要检查元素是否显示。您的元素存在(即DOM的一部分),但它是隐藏的。请注意,您无法在未先检查元素是否存在的情况下检查元素是否显示。如果元素不存在,isDisplayed()方法会抛出错误。

this.checkDropdownPresent = function (dropdownLocator, chooseOption) {
    dropdownLocator.isPresent().then(function (present) {
        if (present) {
            dropdownLocator.isDisplayed().then(function (displayed) {
                if (displayed) {
                    let select = dropdownLocator;
                    select.element(by.cssContainingText('option', chooseOption)).click();
                }
            });
        }
    });
};

答案 1 :(得分:0)

感谢大家的回复。

我解决了将isPresent()更改为isDisplayed()的问题。现在看我的代码:

this.checkDropdownPresent = function (dropdownLocator, chooseOption) {
    dropdownLocator.isDisplayed().then(function(element) {
        if (element) {
            let select = dropdownLocator;
            select.element(by.cssContainingText('option', chooseOption)).click();
        }
    });
};