0
<div class="bodyCells">
      <div style="position:absolute;left:0;">
         <div style="overflow:hidden;">
            <div title="AAA" class="pivotTableCellWrap">AAA</div>
            <div title="BBB" class="pivotTableCellWrap">BBB</div>
         </div>
         <div>
            <div title="AAA-123" class="pivotTableCellWrap">AAA-123</div>
            <div title="BBB-123" class="pivotTableCellWrap">BBB-123</div>        
         </div>
      </div>
</div>

I have two bodycells div in my page and I want the count the nested div inside the second one. Required output :- I want the count=2

Tried Approach :-

int rowCount = driver.FindElements(By.XPath("//div[@class='bodyCells[2]']//div").Count());
Console.WriteLine(rowCount);
undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
jatin patware
  • 141
  • 1
  • 12
  • I had two div of bodycells in my code ..I want the count of nested div for second one as I mentioned above – jatin patware Aug 06 '18 at 12:10
  • Ya it should be outside ..but I still need correct command for it – jatin patware Aug 06 '18 at 12:14
  • Possible duplicate of [XPath query to get nth instance of an element](https://stackoverflow.com/questions/4007413/xpath-query-to-get-nth-instance-of-an-element) – mjwills Aug 06 '18 at 12:15
  • int rowCount = driver.FindElements(By.XPath("//input[@id='bodyCells'])[2]/div/div").Count()); Console.WriteLine(rowCount); Is this will work – jatin patware Aug 06 '18 at 12:25
  • You can use a CSS selector like, `div[title$='-123']`, which will find `DIV`s that have a title that ends with "-123". That's less brittle than your approach in case other elements get added to the list and the index changes. See [CSS selector reference](https://www.w3.org/TR/selectors/#overview). – JeffC Aug 06 '18 at 13:41

2 Answers2

0

As per the HTML you have provided to count the nested child <divs> inside the second (parent) <div> you can use either of the following solution:

  • CssSelector:

    List<string> elements = driver.FindElements(By.CssSelector("div.bodyCells div.pivotTableCellWrap[title*='-']"));
    Console.WriteLine(elements.Count);
    
  • XPath:

    List<string> elements = driver.FindElements(By.XPath("//div[@class='bodyCells']//div[@class='pivotTableCellWrap' and contains(@title,'-')]"));
    Console.WriteLine(elements.Count);
    
undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
  • @jatinpatware Checkout my updated answer and let me know the status – undetected Selenium Aug 06 '18 at 14:11
  • int rowCount = driver.FindElements(By.CssSelector("div.bodyCells div.pivotTableCellWrap[title*='-']")).Count; Console.WriteLine(rowCount); is working properly syntactically but logically not giving correct output – jatin patware Aug 06 '18 at 14:28
0

you can use the below modified XPath inorder to get the count of second nested div

XPath: //div[@class='bodyCells']/div/div[2]/div

Code:

var rowCount = _driver.FindElements(By.XPath("//div[@class='bodyCells']/div/div[2]/div")).Count;
Console.WriteLine(rowCount);
Subburaj
  • 2,294
  • 3
  • 20
  • 37