Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Recommended way to locate parent element in Protractor

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?

like image 760
alecxe Avatar asked Feb 06 '16 05:02

alecxe


People also ask

How do you find the parent element on a protractor?

Find child elements (protractor chaining locators) First we need to find the parent element then using the parent element we find the child element and perform operation on it. var parent = element(by.id('get-input')); var child = parent. element(by. className('form-group')); child.

Which method is used to find all elements in protractor?

Protractor exports a global function element , which takes a Locator and will return an ElementFinder. This function finds a single element - if you need to manipulate multiple elements, use the element. all function. The ElementFinder has a set of action methods, such as click() , getText() , and sendKeys .

Where can I find parent in Selenium?

We can select the parent element of a known element with Selenium webdriver. First of all we have to identify the known element with the help of any of the locators like id, classname and so on. Then we have to identify its parent element with findElement(By. xpath()) method.

How many types of locators are there in protractor?

There are two kinds of locators present in Protractor; I hope you are aware that protractor is nothing but a layer over selenium webdriver.


2 Answers

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 :)

like image 75
Brine Avatar answered Sep 20 '22 15:09

Brine


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

like image 40
quirimmo Avatar answered Sep 16 '22 15:09

quirimmo