【问题标题】:Using Protractor to select a child div id使用量角器选择子 div id
【发布时间】:2016-10-31 20:45:50
【问题描述】:

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

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

<div id="rowId_21">

这是一个 Html sn-p:

<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 对象开始,然后迭代它们:

// 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.');            
});   

我认为这会拉出第一个子 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')]"),

但似乎两者都不起作用。

建议赞赏...

鲍勃

【问题讨论】:

    标签: protractor angularjs-e2e


    【解决方案1】:

    首先,你需要从映射函数中返回。并且,使用.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);            
    });   
    

    【讨论】:

    • 朋友您好,感谢您的回复。原来我需要映射函数中的rowId;但是,看起来我使用thisRowId.getText() 而不是getAttribute("id") 出错了。我会在早上重新测试第一件事。谢谢!
    • @bob.mazzo 明白了,是的,这应该会有所帮助。顺便说一句,如果您以后不需要 id 数组,请查看 each() 是否比 map() 更适合这项工作。谢谢,让我们在城里找个时间喝一两杯啤酒吧! (如果有兴趣可以通过linkedin联系我!)
    • 我正在考虑.each(),但我忘记了到底发生了什么。我会重新考虑这个想法。 +1
    • 在将该关键行更改为getAttribute("id") 后,我注意到该行还提取了行ID - row.element(by.css('div[id^="rowId_"]'))。使用 by.css() 而不是 xpath() 可能会稍微快一点。
    • @bob.mazzo 我想你可能对使用takewhile() 函数感兴趣,看看:stackoverflow.com/questions/32572299/…。希望对您有所帮助。
    猜你喜欢
    • 1970-01-01
    • 2018-01-11
    • 2012-10-07
    • 2019-04-19
    • 1970-01-01
    • 1970-01-01
    • 2010-12-28
    • 1970-01-01
    • 2020-09-14
    相关资源
    最近更新 更多