Reputation: 247
I am receiving the following error on the second pass while iterate through table rows where there is an active delete button.
"Result StackTrace: OpenQA.Selenium.StaleElementReferenceException: stale element reference: element is not attached to the page document"
IWebElement baseTable = Browser.Driver.FindElement(By.XPath("//*[@id='approvalsGrid']/table/tbody"));
ICollection<IWebElement> delButton = baseTable.FindElements(By.XPath("//*[@class = 'k-grid-remove lnkDelete']"));
foreach (var button in delButton)
{
button.Click();
WaitForAjax();
//2nd delete button in popup
Browser.Driver.FindElement(By.XPath("//a[text() = ' Delete']")).Click();
WaitForAjax();
}
Any help will be appreciated.
Upvotes: 1
Views: 3380
Reputation: 1991
It looks like the collection of buttons goes stale when you click delete on one, so the collection is no longer usable once you delete an entry.
In this case you'll likely need to change your strategy to lookup the new delete buttons after you delete one. Something like this might work:
By delButtonsBy = By.XPath("//*[@class = 'k-grid-remove lnkDelete']");
bool delButtonExists = baseTable.FindElements(delButtonsBy).Count > 0;
while (delButtonExists)
{
baseTable.FindElements(delButtonsBy)[0].Click();
WaitForAjax();
//2nd delete button in popup
Browser.Driver.FindElement(By.XPath("//a[text() = ' Delete']")).Click();
WaitForAjax();
delButtonExists = baseTable.FindElements(delButtonsBy).Count > 0;
}
This is more expensive because you have to look up the collection over an over, but you'll have to do that anyway to get the first element because the page structure changes and the collection is out of date every time you delete one.
You could alter this further to just get the first element to check against instead of the entire collection which might improve performance slightly, but it should be pretty quick either way unless you're talking about a very large amount of buttons.
Upvotes: 2