Getting Index was out of range error even when right range used
Posted: Thu Apr 25, 2019 11:49 am
Hi,
I am getting Index was out of range. Must be non-negative and less than the size of the collection. Parameter name: indexerror when executing my code.
I have a 3 div tags and an anchor tag somewhere inside each div. my requirement here is to click on that A tag. I have written below code.
var objEle = Object_Repository.Portfolios.Instance.XYz.Portfolios.test;
DivTag dt = (DivTag)objEle;
IList<DivTag> PCards = dt.Find<DivTag>("//div[@class='portfolios__portfolio-card']"); //getting 3 Devs here without any issue.
for(int i=0;i<PCards.Count;i++)
{
Report.Info("I = ",i.ToString());
PCards.FindDescendants<ATag>()[0].PerformClick();
// Report.Info("Divs",PCards.FindDescendants<ATag>()[0].InnerText.ToString());
Delay.Seconds(5);
repo.XYz.Menu_Items.Portfolios.Click();//I am getting back to main menu to select another A(link)
}
For 2nd iteration its throwing that error. So initially I thought its i value but Im getting the same error if I get my code out of the loop also, as below.
//Code without loop but getting same error when reached to next click.
PCards[0].FindDescendants<ATag>()[0].PerformClick();
Delay.Seconds(5);
repo.XYz.Menu_Items.Portfolios.Click();
PCards[1].FindDescendants<ATag>()[0].PerformClick(); // getting the same error here.
Delay.Seconds(5);
repo.XYz.Menu_Items.Portfolios.Click();
PCards[2].FindDescendants<ATag>()[0].PerformClick();
Delay.Seconds(5);
repo.XYz.Menu_Items.Portfolios.Click();
AND THE CODE IS WORKING VERY FINE if I am clicking only one for the entire test execution i.e. PCards[1].FindDescendants<ATag>()[0].PerformClick(); and PCards[2].FindDescendants<ATag>()[0].PerformClick(); are working fine If I execute a single statement for whole test run, but if Im continuing one by one in single execution Im getting that error.
Any help will be appreciated. Thanks in advance.
I am getting Index was out of range. Must be non-negative and less than the size of the collection. Parameter name: indexerror when executing my code.
I have a 3 div tags and an anchor tag somewhere inside each div. my requirement here is to click on that A tag. I have written below code.
var objEle = Object_Repository.Portfolios.Instance.XYz.Portfolios.test;
DivTag dt = (DivTag)objEle;
IList<DivTag> PCards = dt.Find<DivTag>("//div[@class='portfolios__portfolio-card']"); //getting 3 Devs here without any issue.
for(int i=0;i<PCards.Count;i++)
{
Report.Info("I = ",i.ToString());
PCards.FindDescendants<ATag>()[0].PerformClick();
// Report.Info("Divs",PCards.FindDescendants<ATag>()[0].InnerText.ToString());
Delay.Seconds(5);
repo.XYz.Menu_Items.Portfolios.Click();//I am getting back to main menu to select another A(link)
}
For 2nd iteration its throwing that error. So initially I thought its i value but Im getting the same error if I get my code out of the loop also, as below.
//Code without loop but getting same error when reached to next click.
PCards[0].FindDescendants<ATag>()[0].PerformClick();
Delay.Seconds(5);
repo.XYz.Menu_Items.Portfolios.Click();
PCards[1].FindDescendants<ATag>()[0].PerformClick(); // getting the same error here.
Delay.Seconds(5);
repo.XYz.Menu_Items.Portfolios.Click();
PCards[2].FindDescendants<ATag>()[0].PerformClick();
Delay.Seconds(5);
repo.XYz.Menu_Items.Portfolios.Click();
AND THE CODE IS WORKING VERY FINE if I am clicking only one for the entire test execution i.e. PCards[1].FindDescendants<ATag>()[0].PerformClick(); and PCards[2].FindDescendants<ATag>()[0].PerformClick(); are working fine If I execute a single statement for whole test run, but if Im continuing one by one in single execution Im getting that error.
Any help will be appreciated. Thanks in advance.