如何在量角器中选择单个项目

时间:2021-04-19 01:24:07

Usually in protractor you can select singular element with:

在量角器中,通常可以选择奇异元素:

element(protractor.By.css('#fdfdf'));

Occasionally you get something like this:

有时你会得到这样的结果:

element(protractor.By.css('.dfdf'));

which potentially has more than one element. What's the correct way to select an index from a locator that locates multiple elements, and still contain the protractor's methods for sending Keys?

它可能有多个元素。从定位器中选择索引的正确方法是什么?定位器定位多个元素,并且仍然包含量角器的方法来发送键?

3 个解决方案

#1


68  

You can get an indexed element from an array returned with

可以从返回的数组中获取索引元素

// Get the 5th element matching the .dfdf css selector
element.all(by.css('.dfdf')).get(4).sendKeys('foo');

#2


12  

If you want to get the first element then

如果你想要得到第一个元素

element.all(by.css('.dfdf')).first();
element.all(by.css('.dfdf')).get(0);

#3


2  

Try this one. It will work:

试试这个。它将工作:

element.all(by.css('.dfdf')).get(4).getText();

#1


68  

You can get an indexed element from an array returned with

可以从返回的数组中获取索引元素

// Get the 5th element matching the .dfdf css selector
element.all(by.css('.dfdf')).get(4).sendKeys('foo');

#2


12  

If you want to get the first element then

如果你想要得到第一个元素

element.all(by.css('.dfdf')).first();
element.all(by.css('.dfdf')).get(0);

#3


2  

Try this one. It will work:

试试这个。它将工作:

element.all(by.css('.dfdf')).get(4).getText();