Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to select a single item in protractor

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?

like image 605
user2167582 Avatar asked Nov 26 '13 20:11

user2167582


2 Answers

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'); 
like image 137
Jmr Avatar answered Sep 23 '22 05:09

Jmr


If you want to get the first element then

element.all(by.css('.dfdf')).first(); element.all(by.css('.dfdf')).get(0); 
like image 43
Zaman Afzal Avatar answered Sep 24 '22 05:09

Zaman Afzal