Recommended way to locate parent element in Protractor

10,879

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

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

Share:
10,879
alecxe
Author by

alecxe

"I am a soldier, at war with entropy itself" I am a Software Developer and generalist who is in love with the Python language and community. I greatly value clean and maintainable code, great software, but I know when I need to be a perfectionist and when it stands in a way of product delivery. I like to break things, to find new ways to break things, to solve hard problems, to put things under test and stress, and to have my mind blown by an interesting question. Some of my interests: Learning, Productivity, AI, Space Exploration, Internet of Things. "If you change the way you look at things, the things you look at change." - Wayne Dyer If you are looking for a different way to say "Thank you": Amazon wish list Pragmatic wish list Today I left my phone at home And went down to the sea. The sand was soft, the ocean glass, But I was still just me. Then pelicans in threes and fours, Glided by like dinosaurs, An otter basked upon its back, And dived to find another snack. The sun corpuscular and bright, Cast down a piercing shaft, And conjured an inspiring sight On glinting, bobbing craft. Two mermaids rose up from the reef, Out of the breaking waves. Their siren song was opium grief, Their faces from the grave. The mermaids asked a princely kiss To free them from their spell. I said to try a poet’s bliss. They shrugged and bid farewell. The sun grew dark and sinister, In unscheduled eclipse. As two eight-headed aliens Descended in their ships. They said the World was nice enough But didn’t like our star. And asked the way to Betelgeuse, If it wouldn’t be too far. Two whales breached far out to sea, And flew up to the sky, The crowd was busy frolicking, And didn’t ask me why. Today I left my phone at home, On the worst day, you’ll agree. If only I had pictures, If only you could see. Not everything was really there, I’m happy to confess, But I still have the memories, Worth more than tweets and stress. Today I left my phone at home, I had no shakes or sorrow. If that is what my mind can do, It stays at home tomorrow. Gavin Miller

Updated on June 15, 2022

Comments

  • alecxe
    alecxe almost 2 years

    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?

    • Buaban
      Buaban about 8 years
      As far as I know, xpath with expression ".." is the only way to get parent of the element. I usually use cssSelector to find element but I still have to use xpath to get their parent.
  • Ernst Zwingli
    Ernst Zwingli over 6 years
    I agree with @quirimmo, that this exception is no longer valid. Find here an inofficial doc about parentElementFinder
  • gaccardo
    gaccardo about 2 years
    One other minor note to support this being an exception to avoiding xpath... If you get an element using browser.driver.switchTo().activeElement(), then parentElementArrayFinder is not available, but findElement(by.xpath('..')) is available.