I have a piece of code wrapped in try-catch in order to catch an exception, but I want to re-try that piece of code until it is successful. I have added a loop based on this post. This works as far as looping the code, however it is an infinite loop. Once the code is successful and moves to the next page, the code is still looping and ends up failing because it is searching for a locator that is no longer available because the page has advanced to the next. My question is this: how do I break out of the loop once the code is successful?
int count = 0;
int maxTries = 1;
while (true)
{
try{
driver.FindElement(By.id("textbox").sendKeys("123");
driver.FindElement(By.id("submit").click();
driver.FindElement(By.id("catalog").click();
if(driver.getTitle().equals("Correct Page"))
{break;
}
}
catch(NoSuchElementException e)
{
if (++count == maxTries) throw e;
}
}
}
driver.FindElement(By.id("part1").click();