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?
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.
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 .
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.
There are two kinds of locators present in Protractor; I hope you are aware that protractor is nothing but a layer over selenium webdriver.
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 :)
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();
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