5

Find the elements bellow the ul element, as per the following sample HTML:

<ul _ngcontent-nkg-43="" ngmodelgroup="option">
    <span _ngcontent-nkg-17="" style="cursor: pointer;">Option 1</span>
    <span _ngcontent-nkg-17="" style="cursor: pointer;">Option 2</span>                                                    
    <span _ngcontent-nkg-17="" style="cursor: pointer;">Option 3</span>
</ul>
Giovanni Bassi
  • 739
  • 6
  • 18
jayesh mhatre
  • 77
  • 1
  • 1
  • 6

2 Answers2

14
 var yourParentElement = driver.FindElement(By.XPath(".//ul[ngmodelgroup='option']"));
 var children = yourParentElement.FindElements(By.XPath(".//*"))

This latter call will return all children elements of yourParentElement

Moe Ghafari
  • 2,227
  • 1
  • 12
  • 17
  • 1
    Could you do this via `CssSelector`? – marwaha.ks Sep 08 '16 at 15:38
  • This is a late response, so this is to all those that are seeking a similar issue. cssSelector in this context can be solved via driver.FindElements(By.cssSelector("ul[ngmodelgroup='option'] > span")). https://stackoverflow.com/a/26306203/4415253 – Jadon Steinmetz Apr 11 '21 at 20:14
1

If you're trying to fetch the span elements you could do: driver.FindElement(By.Xpath(".//ul[ngmodelgroup='option']")).FindElements(By.TagName("span"));

Cosmin
  • 2,354
  • 2
  • 22
  • 39