第二,第三,第四,…使用CSS定位器定位量角器中的第八个元素

Locating second, third, fourth,...eigth element in protractor using css locator

本文关键字:量角器 第八个 元素 定位 CSS 第四 第三 使用 第二 定位器      更新时间:2023-09-26

我一直在使用量角器进行测试,除了通过css之外没有办法引用元素,因为它只具有给定的类属性。问题是有超过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();

量角器没有这些功能。只有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

希望能有所帮助。

现在您可以使用.second(); .third(); .fourth();等函数-我开发了一个小包- https://github.com/Marketionist/protractor-numerator -可以与量角器一起使用,以选择第二,第三,第四等元素

使用n -child即

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