protractor Locating Elements Locator basics

Help us to keep this website almost Ad Free! It takes only 10 seconds of your time:
> Step 1: Go view our video on YouTube: EF Core Bulk Extensions
> Step 2: And Like the video. BONUS: You can also share it!

Example

Locators by themselves do not return an element which can be interacted with in Protractor, they are simply instructions that indicate Protractor how to find the element.

To access the element itself, use this syntax:

element(locator);
element.all(locator);

Note: the element(s) is not actually accessed until an action is performed on it - that is, Protractor will only actually go retrieve the element when an action such as getText() is called on the element.

If you want to select only one element using a locator, use element. If your locator points to multiple elements, element will return the first one found. element returns an ElementFinder.

If you want to select multiple elements using a locator, element.all will return all elements found. element.all returns an ElementArrayFinder, and every element in the array can be accessed using different methods - for example, the map function.

element.all(locator).map(function(singleElement) {
        return singleElement.getText();
    }
});

Chaining locators

You can chain multiple locators to select an element in a complex application. You can't directly chain locator objects, you must chain ElementFinders:

element(by.repeater('movie in movies').element(by.linkText('Watch Frozen on Netflix')

There is no limit to how many you chains you can use; in the end, you will still recieve a single ElementFinder or and ElementArrayFinder, depending on your locators.



Got any protractor Question?