Javascript 使用css定位器定位量角器中的第二、第三、第四……个元素

Javascript 使用css定位器定位量角器中的第二、第三、第四……个元素,javascript,angularjs,selenium,jasmine,protractor,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('

我一直在使用量角器进行测试,除了通过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();
使用第n个孩子,即

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

量角器中没有此类函数。只有可用的功能是、和。您可以在
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

希望对您有所帮助。

现在您可以使用
.second()等函数。第三。第四()等-我开发了一个小软件包-可以与量角器一起使用来选择第二、第三、第四等元素