Search code examples
angularjsprotractorangularjs-e2e

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

The answer provided by Florent B. also solves the problem and looks like a much simpler approach.

return content.element(by.cssContainingText('.class', searchName));


Solution

  • You should use the locator by.cssContainingText to find an element by CSS and text:

    return content.element(by.cssContainingText('.class', searchName));