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

后端 未结 5 660
一生所求
一生所求 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:31

    I think that you could refactor your code:

    var linkElements = driver.FindElements(By.CssSelector("a[href]")).ToList();
    var links = new List();
    
    foreach (var elem in linkElements)
    {
        links.Add(elem.GetAttribute("href"));
    }
    

    If that works, you could simplify the query:

    var instantLinks = driver.FindElements(By.CssSelector("a[href]"))
                                    .Select(e => e.GetAttribute("href"))
                                    .ToList();
    

提交回复
热议问题