Kamil
Kamil

Reputation: 892

C# - Selenium - Chromedriver - How to find element in class named the same as another class

I have elements:

<li class="aaa"><div class='bbb'>..content1</div></li >
<li class="aaa"><div class='bbb'>..content2</div></li >
<li class="aaa"><div class='bbb'>..content3</div></li >
...
<li class="aaa"><div class='bbb'>..content4</div></li >

Finding a classes by:

var AllClasses= driver.FindElements(By.CssSelector("li[class='aaa']"));

And then, I'm doing a loop, and trying to check content of specify element:

for (int gr = 0; gr <= AllClasses.Count();gr++) 
{
   var NumberMembers = AllClasses[gr].FindElement(By.XPath("//div[@class='bbb']"));
} 

But it always find me only "bbb" from the first li class. I think that its searching no in specify li from collection from "FindElements" but in whole document.

Can you tell me what to do?

Upvotes: 1

Views: 1836

Answers (1)

cruisepandey
cruisepandey

Reputation: 29362

You can simply use div, cause all divs have same classname.

css selector would be : div.bbb

var AllClasses= driver.FindElements(By.CssSelector("div.bbb"));  

for (int gr = 0; gr <= AllClasses.Count();gr++) 
{
   var NumberMembers = AllClasses[gr].Text;
   Console.WriteLine(NumberMembers);
} 

Upvotes: 2

Related Questions