Home > front end >  Get every ID from HTML-Code which contains certain Css-Value
Get every ID from HTML-Code which contains certain Css-Value

Time:11-18

Im trying to get every ID from HTML-Code which contains certain Css-Value and write them into a List.

I already can get the first ID by using:

List<string> MyList = new List<string>();
int Count = driver.FindElements(By.TagName("My_Value")).Count;

for(int i=0; i<Count; i  )
{
     string ID = driver.FindElement(By.CssSelector("div.My_Value")).GetAttribute("id");

     MyList.Add(ID);
}

The problem is, that i always get the first ID from the HTML-Code althoug MyValue exist in different ID´s. So how can i tell the programm to 'Jump' the allready checked Values?.

CodePudding user response:

The code looks fine, but it is not running a loop, as you say it is finding the 1st element that matches your criteria, then is complete. To get all of them, try:

string ID = driver.FindElements(By.CssSelector("div.My_Value")).GetAttribute("id");

Not tried this myself , but give it a go

CodePudding user response:

If you are constructing the list using By.TagName("My_Value") only the <My_Value> elements will be collected and put into the list, i.e.

<My_Value>class="foo bar"...</My_Value> 

Moving forward, within the loop FindElement(By.CssSelector("div.My_Value")) identifies only the <div> elements with class attribute as My_Value i.e.

<div>class="My_Value"...</div>

So the outer FindElements() and innerloop FindElement() doesn't match. Hence you don't get all the values.


Solution

To iterate over all the collected list elements you need to use:

string ID = driver.FindElement(By.TagName("My_Value")).GetAttribute("id");
  • Related