Recommended way to locate parent element in Protra

2019-06-15 01:34发布

问题:

According to the newly published Style Guide, using the by.xpath() locators is considered a bad practice. I'm actually trying to follow the suggestion, but stuck on getting a parent element.

Currently we are using .. XPath expression to get to the element's parent:

this.dashboard = element(by.linkText("Dashboard")).element(by.xpath("..")); 

How can I locate the element's parent using other built into Protractor/WebDriverJS locators?

回答1:

While I dig the Style Guide, and agree that xpath is to be avoided, there's always an exception that proves the rule. I think this is one of those cases :)



回答2:

Actually, at the moment there is an easier way to select the parent of an element avoiding to use xpath. From an ElementFinder you can simply access the parent element through parentElementArrayFinder and then for example trigger the click method:

myElement.parentElementArrayFinder.click();