protractor 4 : how to locate a parent at 'n' level above by class

protractor

Solution

You have mentioned incorrect locator combination. That is incuded css value '.parent' in xpath expression. The right way is:

   let parent = child.element(by.xpath('ancestor::div')); 
OR 
   let parent = child.element(by.xpath('ancestor::div[1]'));  

Problem

For instance with following html: ``` <div class='parent'> <p> <span class='child'></span> </p> </div> ``` To locate .parent from .child I'm trying something like : ``` let child = element(by.css('.child')); let parent = child.element(by.xpath('../..')); // not working let parent = child.element(by.xpath('ancestor::.parent')); // not working ``` What's the best way to achieve that?

Original source