Search code examples
javascriptangularjsprotractorangularjs-e2e

Looking to get index of first matched element within array


I'm looking to get index position of matched element within repeater and would like to know how I can find.

List of books below

element.all(by.repeater('books'))

within those rows, some rows will have below element

element(by.css('[ng-click="download"]')

and I want to do following

  1. find element of download button (more than 1)
  2. get index position of first download button (so I can do other stuffs later with .get(3) for specific row)

So here what I have tried.

element.all(by.css('some-css')).first().element(by.tagName('tag-within-css')).get(index);
element.all(by.css('some-css')).get(index).element(by.tagName('tag-within-css'));

Finding Sub-Elements - Protractor locators guide

some error such as index not defined or no method of 'get'.

Thanks.


Solution

  • I think you don't need to operate using element positions/indexes inside the repeater. Instead, find download buttons and operate with the current row using context/element specific element searches by chaining element and element.all:

    var books = element.all(by.repeater('books'));
    
    var downloadButtons = books.all(by.css('[ng-click="download"]'));
    var firstDownloadButton = downloadButtons.first();
    
    // here is how you can, for example, get the next div element to the download button  
    var nextElement = firstDownloadButton.element(by.xpath('following-sibling::div'));