使用量角器 select 一个 child div id

Using Protractor to select a child div id

我在为下面的 row.map 函数中的每一行提取 child div ID 时遇到问题。

换句话说,对于我迭代的每个行元素,我想拉 rowId attrib:

<div id="rowId_21">

这是一个 Html 片段:

<div ng-repeat="row in vm.sourceData.data" ng-init="thatRow=$index">
 <div id="rowId_21" ng-class-odd="'row-2'" ng-class-even="'row-3'" >
   <div ng-repeat="column in  vm.sourceData.columns" >    
     <div ng-if="row[column.field].length !== 0" class="ng-scope highlight21"> 
   <span ng-bind-html="row[column.field] | changeNegToPrenFormat" vm.highlightedrow="" class="ng-binding"> 
                Sales
         </span>
     </div>    
   </div>
</div>
</div> 
<div ng-repeat="row in vm.sourceData.data" ng-init="thatRow=$index">
  <div id="rowId_22" ng-class-odd="'row-2'" ng-class-even="'row-3'" ng-class="vm.hideRow(row)" class="row-3 height-auto">
  <!-- ... -->
 </div>
</div>

我从拉 rows object 开始,然后迭代它们:

// pulls the data rows
var selDataRows = '.grid-wrapper.fluid-wrapper';
var rows = element(by.css(selDataRows)).all(by.repeater('row in vm.sourceData.data'));

rows.map(function (row, idx) {            
  //var thisRowId = row.element(by.css('div[id^="rowId_"]'));  // *** RETURNS NULL
  
  var thisRowId = row.all(by.xpath("descendant::div[starts-with(@id, 'rowId')]"));
  
  thisRowId.getText().then(function(txt){              
      // RETURNS A VERY LONG ID: [ '7\n4\n41\n113\n3.3\n(34)\n(1.1)\n7...]
      console.log('--- Curr Div ID: ', txt);
  });
  
}).
then(function(){            
console.log('*** Rows.Map Done.');            
});   

我认为这会拉出第一个 child id(即 https://developer.mozilla.org/en-US/docs/Web/CSS/Attribute_selectors ):

by.css('div[id^="rowId_"]') ,

或者这样:

by.xpath("descendant::div[starts-with(@id, 'rowId')]") ,

但是似乎都不起作用。

感谢您的建议...

鲍勃

首先,你需要从映射函数return。并且,使用 .getAttribute() 方法获取 id 属性:

var selDataRows = '.grid-wrapper.fluid-wrapper';
var rows = element(by.css(selDataRows)).all(by.repeater('row in vm.sourceData.data'));

rows.map(function (row) {            
    return row.element(by.xpath("descendant::div[starts-with(@id, 'rowId')]")).getAttribute("id").then(function(rowId) {              
        return rowId;
    });
}).then(function(ids) {            
    console.log(ids);            
});