I got an inconsistent scenario where sometimes I got the error “Element Is No Longer Attached To The DOM”.
To deal with this I have added the following code while accessing the element:
int count = 0;while (count < 5){try{IWebElement textFound = driver.FindElement(By.LinkText(text)); textFound.Click();}catch (StaleElementReferenceException e) {e.ToString();Console.WriteLine(“Trying to recover from stale element:” + e.Message);count = count + 1;}count = count + 5;}
Thus, instead of failing straightaway, the test will attempt a few times to click the element, or else exception will be caught to allow the test to fail gracefully.
Leave a Reply