Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get the parent of an element

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
});
like image 911
rsboarder Avatar asked Jan 20 '14 15:01

rsboarder


People also ask

How do you find the parent of an element?

To get the parent node of an HTML element, you can use the parentNode property. This property returns the parent node of the specified element as a Node object. The parentNode property is read-only, which means you can not modify it.

What is the parent of an element?

A parent is an element that is directly above and connected to an element in the document tree. In the diagram below, the <div> is a parent to the <ul>. A child is an element that is directly below and connected to an element in the document tree. In the diagram above, the <ul> is a child to the <div>.

What is used to get the direct parent of an element?

The parent() method returns the direct parent element of the selected element. The DOM tree: This method only traverse a single level up the DOM tree. To traverse all the way up to the document's root element (to return grandparents or other ancestors), use the parents() or the parentsUntil() method.


2 Answers

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()).

like image 168
kamituel Avatar answered Oct 02 '22 06:10

kamituel


Decided to use xpath.

var row = results[randomNum].findElement(by.xpath('ancestor::tr'));
like image 22
rsboarder Avatar answered Oct 02 '22 06:10

rsboarder