taguenizy taguenizy - 2 months ago 16
AngularJS Question

Protractor retrieve single element from ElementArrayFinder

I want to retrieve the first element from a ElementArrayFinder matching a condition. The following code provides me with the element that I'm expecting but it goes through all elements which takes time.

Is there any way to this?

return content.all(by.css('.class')).filter(function(label){
return label.getText().then(function(text){
return text === searchName;
});
}).first();


CONCLUSION:
Answer provided by Sudharsan Selvaraj worked as a charm

var ele = content.all(by.xpath(".//*[contains(@class,'class')][normalize-space(text())='some-value']")).first()


Reduced drastically time searching the element

Answer

You can use xpath to find the element with specific text.Look at below sample xpath.

var ele = content.all(by.xpath(".//*[contains(@class,'class')][normalize-space(text())='some-value']")).first()