使用css定位器在量角器中定位第二,第三,第四,......第八个元素

时间:2015-10-22 11:21:19

标签: javascript angularjs selenium jasmine protractor

我一直在测试使用量角器,除了通过css之外没有办法引用元素,因为它只提供了class属性。问题是有超过7个元素具有此类名称。因此我使用语法

element.all(by.css('h4.ng-binding')).first(); 

对于第一个并且它工作正常但对其他人来说它不起作用!我使用的逻辑也与第一个逻辑相同。这是我的代码片段,供其他人找到它们。

  element.all(by.css('h4.ng-binding')).second();
  element.all(by.css('h4.ng-binding')).third();
  element.all(by.css('h4.ng-binding')).fourth();
  element.all(by.css('h4.ng-binding')).fifth();
  element.all(by.css('h4.ng-binding')).sixth();
  element.all(by.css('h4.ng-binding')).seventh();
  element.all(by.css('h4.ng-binding')).eighth();

3 个答案:

答案 0 :(得分:12)

量角器中没有这类功能。只有first()last()get()可用的功能。您可以在ElementArrayFinder之上使用这3个函数实现所有功能。这是怎样的 -

element.all(by.css('h4.ng-binding')).first(); //get the first element
element.all(by.css('h4.ng-binding')).get(0); //get the first element as get() function is a '0' based index
element.all(by.css('h4.ng-binding')).get(1); //get the second element
element.all(by.css('h4.ng-binding')).get(2); //get the third element
element.all(by.css('h4.ng-binding')).last(); //get the last element

希望它有所帮助。

答案 1 :(得分:3)

现在你可以使用.second(); .third(); .fourth();等函数了。 - 我开发了一个小包 - REPL Link - 可以与量角器一起使用来选择第二,第三,第四等元素。

答案 2 :(得分:-3)

使用nth-child,即

element.all(by.css('h4.ng-binding:nth-child(2)'))...