2021年1月1日星期五

Selenium: How to find any ancestor based on one of ancestor's attribute using IWebElement.FindElement()

Let's say I have this HTML

<div class="item-wrapper">   <div>    <h6>My Header</h6>                 <div>   <div>    <div>     <label>      <input type="checkbox" />      <span class="class1">Text 1</span>             <span class="class2"></span>     </label>    <div>   </div>  </div>  

How do I get to any child to the common parent, i.e. <div class="item-wrapper">? In this case, the attribute is the class. However, the attribute could be anything that can identify the common ancestor.

var xPathToAncestor = "ancestor::div[@class='item-wrapper']";  var ancestor = child.FindElement(By.XPath(xPathToAncestor)  

I've tried so many combinations //ancestor::div[@class='item-wrapper'], .//ancestor::div[@class='item-wrapper'], but nothing is working.

Thanks for helping

https://stackoverflow.com/questions/65535496/selenium-how-to-find-any-ancestor-based-on-one-of-ancestors-attribute-using-iw January 02, 2021 at 10:17AM

没有评论:

发表评论