for loop “index was out of range” c# webdriver

后端 未结 5 659
一生所求
一生所求 2021-01-16 07:08

I am getting \"index out of range\" from this loop. But I need to use new elements that loop founds, how do I do that? Please help to fix the problem

int lin         


        
5条回答
  •  栀梦
    栀梦 (楼主)
    2021-01-16 07:37

    int linkCount = driver.FindElements(By.CssSelector("a[href]")).Count;
    List links = new List();
    
    for (int i = 0; i < linkCount; i++)
    {
        List linksToClick = driver.FindElements(By.CssSelector("a[href]")).ToList();
        if (linksToClick.Count < i)
            links.Add(linksToClick[i].GetAttribute("href"));
    }
    

    This might help with the out of range exception. Doing this allows you to create a list of type: string without having to explicitly define the size of the list

提交回复
热议问题