javascript - 使用 css 定位器在 Protractor 中定位第二个、第三个、第四个……第八个元素

标签 javascript angularjs selenium jasmine protractor

我一直在使用 Protractor 进行测试,除了通过 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();

最佳答案

Protractor 中没有此类函数。只有可用的功能是 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

希望对您有所帮助。

关于javascript - 使用 css 定位器在 Protractor 中定位第二个、第三个、第四个……第八个元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33279813/

相关文章:

javascript - JSON 文件递归迭代返回 'undefined' 和找到的值

angular - 指定 Protractor 应使用我手动下载的 chromedriver

javascript - Selenium 二维数组

c# - 如何在 C# 中使 webdriver 线程安全?

javascript - 我可以使用 jQuery globaleval 而不是 eval 来逃避内容安全策略吗

javascript - 如何在 HTML 中做列表到网格的动画过渡?

javascript - Dojo 在 dnd/drop/before 中中止拖放操作

javascript - 同时使用 ng-show 和 ng-repeat

javascript - RxJS 可观察到的.concat : How to know where next result came from?

javascript - Angular JS ng-repeat以垂直分割格式排列