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
});
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.
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>.
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.
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()
).
Decided to use xpath.
var row = results[randomNum].findElement(by.xpath('ancestor::tr'));
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With