最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Locating parent elements with parentElementArrayFinder - Stack Overflow

programmeradmin1浏览0评论

There is a parentElementArrayFinder attribute available on an ElementFinder object which, from I understand, may return a parent element of the current:

var myElement = $(".myclass");
var parentElement = myElement.parentElementArrayFinder;

It is, though, not documented as a part of Protractor's public API. Is parentElementArrayFinder a reliable and stable method to locate a parent element and would always return the same element as myElement.element(by.xpath(".."))?

There is a parentElementArrayFinder attribute available on an ElementFinder object which, from I understand, may return a parent element of the current:

var myElement = $(".myclass");
var parentElement = myElement.parentElementArrayFinder;

It is, though, not documented as a part of Protractor's public API. Is parentElementArrayFinder a reliable and stable method to locate a parent element and would always return the same element as myElement.element(by.xpath(".."))?

Share Improve this question asked Aug 14, 2017 at 13:18 alecxealecxe 474k127 gold badges1.1k silver badges1.2k bronze badges 5
  • Help me understand why you can't use by.xpath("..")? – Michael Warner Commented Aug 30, 2017 at 21:19
  • @MichaelWarner I can and do use .. here and there; parentElementArrayFinder is just something that caught my eye and I wonder what purpose does it have and if it can be used. Thanks. – alecxe Commented Aug 30, 2017 at 21:29
  • 2 If i remember correctly parentElementArrayFinder holds the methods to retrieve the web elements from a chained locator/filter like $().$() or $$().filter(). It's not related to the parent element from the DOM. – Florent B. Commented Aug 31, 2017 at 15:26
  • @FlorentB. right, that was my thinking initially but I figured to ask that to confirm. Please post as an answer to resolve the topic. Thanks. – alecxe Commented Aug 31, 2017 at 15:33
  • Check this link as it is a similar kind of problem. github./angular/protractor/blob/… – abhishek Sharma Commented Sep 5, 2017 at 11:36
Add a ment  | 

2 Answers 2

Reset to default 2

I found the code for the ElementFinder here which has a prop for parentElementArrayFinder here.

From what I have found in the code. It throws an error if parentElementArrayFinder does not exist.

super();
if (!elementArrayFinder) {
  throw new Error('BUG: elementArrayFinder cannot be empty');
}
this.parentElementArrayFinder = elementArrayFinder;

From this, we can safely assume it will always be there so I think it is safe to use.

To summarize ments, the existing answer, my observations and the source code, parentElementArrayFinder should and can only be used if the element you get parentElementArrayFinder for was "chained" from an element.

This will work:

var parent = element(by.css(".parent"));
var child = parent.element(by.css(".child"));

child.parentElementArrayFinder  # link to "parent" - defined

This will not:

var child = element(by.css(".child"));

child.parentElementArrayFinder  # no link to "parent" - undefined
发布评论

评论列表(0)

  1. 暂无评论