量角器4:如何按类定位上面'n'级的父级

seb*_*bap 3 protractor

例如,使用以下html:

<div class='parent'>
  <p>
    <span class='child'></span>
  </p>
</div>
Run Code Online (Sandbox Code Playgroud)

从.child找到.parent我正在尝试这样的事情:

let child = element(by.css('.child'));
let parent = child.element(by.xpath('../..')); // not working
let parent = child.element(by.xpath('ancestor::.parent')); // not working
Run Code Online (Sandbox Code Playgroud)

实现这一目标的最佳方法是什么?

Opt*_*rks 5

您提到了错误的定位器组合.这在xpath表达式中包含了css值'.parent'.正确的方法是:

   let parent = child.element(by.xpath('ancestor::div')); 
OR 
   let parent = child.element(by.xpath('ancestor::div[1]'));  
Run Code Online (Sandbox Code Playgroud)