我一直在使用 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/