-1

I'm getting the following error when executing selenium C# code in my test script

System.ArgumentOutOfRangeException: Index was out of range. Must be non-negative and less than the size of the collection. (Parameter 'index') at System.Collections.Generic.List`1.get_Item(Int32 index)

What does this mean? What is the problem in my code?

// Access the project
            IList<IWebElement> allRows = driver.FindElements(By.XPath("//*[@id='GridViewProjList_ctl00']/tbody/tr"));
            IList<IWebElement> allPages = driver.FindElements(By.XPath("//div[@class='rgWrap rgNumPart']//a"));
            string projectName = "TITAN";

            for (int i = 0; i <= (allPages.Count); i++)
            {

                allRows = driver.FindElements(By.XPath("//*[@id='GridViewProjList_ctl00']/tbody/tr"));
                for (int row = 1; row <= (allRows.Count); row++)
                {

                    projectName = "TITAN";
                    IWebElement nameElement = driver.FindElement(By.XPath("//table/tbody/tr/td[2]/div/div/div/table/tbody/tr[3]/td/div/div/div/div[2]/table/tbody/tr[" + row + "]/td[1]"));
                    string name = nameElement.Text;

                    if (projectName.Contains(name))
                    {

                        nameElement = driver.FindElement(By.XPath("//table/tbody/tr/td[2]/div/div/div/table/tbody/tr[3]/td/div/div/div/div[2]/table/tbody/tr[" + row + "]/td[1]"));
                        nameElement.Click();
                        break;

                    }

                    allPages = driver.FindElements(By.XPath("//div[@class='rgWrap rgNumPart']//a"));
                }

                allPages = driver.FindElements(By.XPath("//div[@class='rgWrap rgNumPart']//a"));
                driver.Manage().Timeouts().PageLoad = TimeSpan.FromSeconds(5);
                allPages.ToList()[i].Click();
                Thread.Sleep(3000);

            }

            Thread.Sleep(3000);
            Console.WriteLine($"{projectName} project has been successfully accessed");
            Thread.Sleep(3000);
Guru Stron
  • 102,774
  • 10
  • 95
  • 132
Dilshan Kay
  • 11
  • 1
  • 1

1 Answers1

0

The thing is, your list size is not guaranteed to be the same during iteration:

The iterator is based on the allPages:

for (int i = 0; i <= (allPages.Count); i++)

Also note Guru Stron's comment here and correct the iterator bounds:

Also there is error in the length check, cause collections are zero-based it should be for (int i = 0; i < (allPages.Count); i++)

But the list is overridden multiple times, e.g.:

allPages = driver.FindElements(By.XPath("//div[@class='rgWrap rgNumPart']//a"));

And here you use the original indexer again:

allPages.ToList()[i].Click();

If the new list item count is less than i-1, you'll run into this error.


The solution is to not overwrite the list, or check the length when accessing the i-th element.

Stefan
  • 17,448
  • 11
  • 60
  • 79
  • 1
    Also there is error in the length check, cause collections are zero-based it should be `for (int i = 0; i < (allPages.Count); i++)` – Guru Stron Jun 24 '20 at 12:26
  • @GuruStron: nice catch. – Stefan Jun 24 '20 at 12:29
  • I tried it. The same error is coming – Dilshan Kay Jun 24 '20 at 12:31
  • @DilshanKay: what did you try exactly? – Stefan Jun 24 '20 at 12:36
  • If I do not use "allPages = driver.FindElements(By.XPath("//div[@class='rgWrap rgNumPart']//a"));" multiple times, the following error is given "OpenQA.Selenium.StaleElementReferenceException: stale element reference: element is not attached to the page document" – Dilshan Kay Jun 24 '20 at 12:37
  • I understand; but you are changing the list while iterating over it. You should avoid that at all cost. The other errors should be resolvable. – Stefan Jun 24 '20 at 13:03