Search code examples
c#seleniumselenium-webdrivergetattribute

Get every ID from HTML-Code which contains certain Css-Value


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?.


Solution

  • I figured it out.

    You have to use a var variable to safe all the element´s. And get the ID´s through a foreach Loop.

    List<string> MyList = new List<string>();
    
    var ID = driver.FindElement(By.CssSelector("div.My_Value"));
    
    foreach(IWebElement element in ID)
    {
         element.GetAttribute("id")
         MyList.Add(ID);
    }