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

Eyo*_*ele 10 javascript selenium jasmine angularjs protractor

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

element.all(by.css('h4.ng-binding')).first(); 
Run Code Online (Sandbox Code Playgroud)

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

  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();
Run Code Online (Sandbox Code Playgroud)

Gir*_*tur 16

量角器中没有这类功能.只有可用的功能是first(),last()get().你可以使用这3个函数来实现所有功能ElementArrayFinder.这是如何做 -

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
Run Code Online (Sandbox Code Playgroud)

希望能帮助到你.


jpi*_*etz -3

使用第 n 个孩子即

element.all(by.css('h4.ng-binding:nth-child(2)'))...
Run Code Online (Sandbox Code Playgroud)