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

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

    You can rewrite your code to bypass the for loop:

    string[] links = driver.FindElements(By.CssSelector("a[href]")).Select(l => l.GetAttribute("href")).ToArray();
    

    This should also avoid the index out of range problem, and cut down the amount of code you have to write.

    0 讨论(0)
  • 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<string>();
    
    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();
    
    0 讨论(0)
  • 2021-01-16 07:33

    the first one gets all of your elements by tag name ...let's assume 5.

    in the loop, your driver get's all the elements by css selector, and you might have a different number here. let's say 4.

    then, you might be trying to set the fifth element in a four element array. boom.

    Easiest fix to debug:

    int linkCount = driver.FindElements(By.TagName("a")).Count;
    string[] links = new string[linkCount];
    // WRITE OUT HOM MANY links you have
    
    for (int i = 0; i < linkCount; i++)
    {
        List<IWebElement> linksToClick = driver.FindElements(By.CssSelector("a[href]")).ToList();
        // ASSERT THAT YOU HAVE THE SAME AMOUNT HERE
        If (links.Count != linksToClick.Count)
             // your logic here
    
        links[i] = linksToClick[i].GetAttribute("href");
    }
    
    0 讨论(0)
  • 2021-01-16 07:34

    First of all i dont see a point in assigning linkstoclick values inside loop... And Reason for error must be that linksToClick list's length is more than that of linkCount.

    0 讨论(0)
  • 2021-01-16 07:37
    int linkCount = driver.FindElements(By.CssSelector("a[href]")).Count;
    List<string> links = new List<string>();
    
    for (int i = 0; i < linkCount; i++)
    {
        List<IWebElement> 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

    0 讨论(0)
提交回复
热议问题