How to get the parent of an element

rsboarder picture rsboarder · Jan 20, 2014 · Viewed 26.3k times · Source

For example, I am randomly picking a button element from within the rows of a table.
After the button is found, I want to retrieve the table's row which contains a selected button.

Heres is my code snippet:

browser.findElements(by.css('[ng-click*=submit]')).then(function (results) {
  var randomNum = Math.floor(Math.random() * results.length);
  var row = results[randomNum];
         // ^ Here I want to get the parent of my random button
});

Answer

kamituel picture kamituel · Feb 9, 2015

As of the most recent Protractor (1.6.1 as of this writing), the syntax changed a bit:

var row = results[randomNum].element(by.xpath('..'));

(use element() instead of findElement()).