javascript - 类型错误 : Cannot call method 'click' of undefined

标签 javascript angularjs selenium promise protractor

我想通过第二列值从表中选择特定元素(我删除了呈现的空格),找到该元素后我想单击它(返回值为 true)。我已经尝试过这个,但它不会点击它,而只是找到该元素。

我要选择的字段的 html 代码如下:

<table ng-table="tableParams" id="Panel" class="tbl-option-list" template-pagination="directives/controls/Pager/Pager.html">
    <caption translate>Orders</caption>
    <tr id="Panel">
        <!-- 1 -->
        <th class="fixed-width-glyphicon"></th>
        <!-- 2 -->
        <th translate>Identifier</th>
    </tr>
        <tr ng-repeat="item in $data track by $index" ng-class="{'active-bg': order.$selected}" ng-click="changeSelection(order,  getRowActions(order))">
        <!-- 1 -->
        <td class="fixed-width-glyphicon">
            <div class="fixed-width-glyphicon">
                {{item.priority.toUpperCase()[0]}}
            </div>
        </td>
        <!-- 2 -->
        <td>{{item.identifierCode}}</td>
    </tr>
</table>

Protractor 的选择命令是:

var deferred = protractorData.p.promise.defer();
element.all(by.repeater('item in $data track by $index')).filter(function(row) {
    row.getText().then(function(txt) {
        txt = txt.replace(/\s/g, '');
        var found = txt.split('ID0001');
        return found.length > 1;
    });
}).then(function(elem) {
        deferred.fulfill(elem[0]);
    });
    return deferred.promise;
}

我收到以下错误:

TypeError: Cannot call method 'click' of undefined.

最佳答案

似乎该元素没有返回以供单击。尝试下面的示例来单击并查看它在过滤器函数中是否正常工作,而不是使用 Promise 返回元素 -

element.all(by.repeater('item in $data track by $index')).filter(function(row) {
    //return found element
}).then(function(elem) {
    elem[0].click(); //click it here
});

或者通过以下方式返回元素,然后在测试规范中单击它。方法如下 -

var clickElement = element.all(by.repeater('item in $data track by $index')).filter(function(row) {
    //return found element
}).then(function(elem) {
    return elem[0]; //return the element
});
return protractor.promise.fulfilled(clickElement);

希望有帮助。

关于javascript - 类型错误 : Cannot call method 'click' of undefined,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32778709/

相关文章:

javascript - 如何使用javascript更改表格中单元格的背景颜色

javascript - 在系统浏览器中打开来自 JSON 的链接 - Cordova

angularjs - 使用 Docker/Dokku 部署 Angular 应用程序

c# - Selenium C#超时异常

javascript - 减少通过函数传递许多对象的重复

javascript - 用切片复制数组时的奇怪行为

python - python Selenium 包中的格式错误

python - 如何使用 python selenium 循环 html 元素以单击页面中的每个元素?

javascript - 单选按钮不会以 html 形式显示

angularjs - 加入现有的 Angular 应用以表达后端