I am trying to wait for an element on a page to be populated with inner text, which occurs after the page is refreshed. Obviously if the wait function checks the content of the element while the page is refreshing it will throw a stale element exception. I have enclosed this inside of a try/catch block to catch this exception and handle it but Visual Studio is still breaking and saying it is not handled.
Here is the code:
int attempts = 0;
while(attempts < 2) {
try
{
wait = new WebDriverWait(edgeDriver, TimeSpan.FromSeconds(30));
wait.Until(d => d.FindElement(By.Id("lblFormatedIncidentID")).GetAttribute("innerText") != "");
incidentId = edgeDriver.FindElement(By.Id("lblFormatedIncidentID")).GetAttribute("innerText");
break;
}
catch (StaleElementReferenceException)
{
Debug.Print("Failed to acquire Incident ID - Stale element error. Retrying...");
attempts++;
if (attempts == 2)
{
throw;
}
If I press continue in the debugger then it will catch the exception and handle it as expected. Does anyone know why this is happening and how to prevent it?