代码之家  ›  专栏  ›  技术社区  ›  Alexandre

函数在第一次成功迭代后崩溃(元素未附加到页面文档)

  •  0
  • Alexandre  · 技术社区  · 7 年前

    想问一下下面的函数有什么问题,第一次迭代没有任何问题,但是在网格刷新之后,当量角器试图移动到下一个单元格时,它会给我这个错误消息:

    失败:过时的元素引用:元素未附加到页文档

    这样做的目的是读取第5列(如果考虑从0开始,则为第4列),选中包含值“true”的每一行,如果包含true,则执行编辑该行的操作,单击复选框并保存(所有操作都在该行执行)

    **每次更改和保存值时,表都会刷新

        function resetGoodItemStatus(siteToResetValues){
          var cellsConform = element.all(by.css('#datatableDir tr td:nth-of-type(5)'));
          var conformCounter = 0;
          selectValueDropDown(siteToResetValues)
            cellsConform.each((eachCell) => {
                eachCell.getText().then((cellText) => {
                    switch (cellText)
                    {
                        case 'true':
                            element(by.id(conformCounter+'-1')).getText().then(function(value){
                                element(by.id('btnEdit-US.'+value)).click();
                                element(by.xpath("//*[@editable-checkbox=\"scopeValue.getConformMapping(scopeValue.getDataRow("+'\'US.'+value+"\')).conform\"]/../span/span/input")).click();
                                element(by.id('btnSubmit-US.'+value)).click();
                            })
                        default:
                            browser.sleep(100)
                    }
                    conformCounter += 1
                    });
    
            });
          }
    

     <tr role="row" class="odd">
     <td id="0-0" class="ng-scope">
    <form editable-form="" name="scopeValue.rowforms['US.DAM']" onaftersave="scopeValue.saveData('US.DAM',0)" ng-show="scopeValue.rowforms['US.DAM'].$visible" class="form-buttons form-inline ng-pristine ng-valid ng-hide" style="">
        <button type="button" class="btn btn-xs btn-trans kni kni-check-circle text-info" id="btnSubmit-US.DAM" ng-disabled="scopeValue.rowforms['US.DAM'].$waiting" ng-click="scopeValue.rowforms['US.DAM'].$submit()">
        </button>
        <button type="button" class="btn btn-link kni kni-x-circle-slim" ng-disabled="scopeValue.rowforms['US.DAM'].$waiting" id="btnCancel-US.DAM" ng-click="scopeValue.cancelData('US.DAM',0)">
        </button>
    </form>
    <div class="buttons" ng-show="!scopeValue.rowforms['US.DAM'].$visible">
        <button type="button" class="btn btn-xs btn-trans kni kni-edit-circle text-info" ng-click="scopeValue.rowforms['US.DAM'].$show()" id="btnEdit-US.DAM">
        </button>
    </div></td>
    <td id="0-1" class="ng-scope sorting_1">DAM</td>
    <td id="0-2" class="ng-scope">US.DAM</td>
    <td id="0-3" class="ng-scope">Generic Damaged Code</td>
    <td id="0-4" class="ng-scope">
    <span editable-checkbox="scopeValue.getConformMapping(scopeValue.getDataRow('US.DAM')).conform" e-name="conform" e-form="scopeValue.rowforms['US.DAM']" e-required="" class="ng-scope ng-binding editable">false</span>
    </td></tr>
    

    点击编辑按钮后的HTML:

    <td id="0-4" class="ng-scope">
    <span editable-checkbox="scopeValue.getConformMapping(scopeValue.getDataRow('US.DAM')).conform" e-name="conform" e-form="scopeValue.rowforms['US.DAM']" e-required="" class="ng-scope ng-binding editable editable-hide">false</span>
    <span class="editable-wrap editable-checkbox ng-scope">
        <span class="editable-controls"><input type="checkbox" name="conform" required="required" class="editable-input ng-pristine ng-empty ng-invalid ng-invalid-required ng-touched" ng-model="$data" style="">
            <div class="editable-error ng-binding ng-hide" ng-show="$error" ng-bind="$error" style=""></div>
        </span>
    </span>
    </td>
    

    谢谢你抽出时间!

    1 回复  |  直到 7 年前
        1
  •  1
  •   yong    7 年前

    一种简单的方法是将第5列中所有单元格的文本转换成文本数组,然后遍历文本数组,数组索引相当于表行索引。

    true ,使用行索引查找表行。其余元素可以在表行中找到。

    因为在每次迭代中,下面的代码将再次从页中查找所有表行,不应该发生 Stale Exception

    function resetGoodItemStatus(siteToResetValues){
        var cellsConform = element.all(by.css('#datatableDir tr td:nth-of-type(5)'));
    
        selectValueDropDown(siteToResetValues);
    
        cellsConform.getText().then(function(conforms) {
    
            // conforms is a string Array, each one is the text of one cell of 5th column
            conforms.forEach(function(conform, rowIndex) {
    
                if(conform === 'true') {
                    var row = element.all(by.css('#datatableDir tr').get(rowIndex);
    
                    row.element(by.css('button[id^="btnEdit-US"]')).click();
    
                    row.element(by.css('input[type="checkbox"]')).click();
    
                    row.element(by.css('button[id^="btnSubmit-US"]')).click();
    
                    browser.sleep(3000)
                }
            });
        });
    
    }