I am new to selenium coding, and I have the below code where I am fetching values from the table it has multiple pages, for 1st time, it reads all values from the table and control move to the next page, I m getting the error stale element reference: element is not attached to the page document but when i m debugging the code, i m not getting any error for the below code, when i run it throws an error and it shows an error at line where I have defined tdCollection
Please guide me on this.
var ReportCount = Convert.ToInt32(_driver.FindElement(By.Id("Reporter_TotalPages")).Text);
for (int i = 0; i < ReportCount; i )
{
IList<IWebElement> _records = (IList<IWebElement>)_driver.FindElements(By.XPath("//*[contains(@id,'ReportViewerControl')]//div//table//tbody//tr[position()>2]"));
IList<IWebElement> tdCollection;
for (int j = 0; j < _records.Count; j )
{
tdCollection = _records[j].FindElements(By.TagName("td"));
var Patientdemolist = new XPatientDemographicsList();
{
Patientdemolist.PatientID = tdCollection[0].Text;
Patientdemolist.LastName = tdCollection[1].Text;
Patientdemolist.FirstName = tdCollection[2].Text;
};
PatientDemographicsList.Add(Patientdemolist);
tdCollection = null;
}
if (ReportCount - 1 > i)
{
// For Next Page
_driver.FindElement(By.Id("Report_Next")).Click();
}
}
CodePudding user response:
Try adjusting your conditional to this.
if (ReportCount - 1 > i)
{
// For Next Page
_driver.FindElement(By.Id("Report_Next")).Click();
Thread.Sleep(5000)
}
Its possible you are getting a reference before the page has completed loading from the .Click() method. If that works you can refine the tests to wait implictly/ use fluent waits instead of waiting for 5 seconds. https://www.selenium.dev/documentation/webdriver/waits/
CodePudding user response:
yes I tried this one and also I tried the below loop for both _records and tdcollection but still facing the same error
do
{
_driver.Manage().Timeouts().PageLoad.Add(System.TimeSpan.FromSeconds(30));
tdCollection = _records[j].FindElements(By.TagName("td"));
}
while (tdCollection.Count <= 0);