The best option is to understand and resolve the underlying error condition.
If the error is only something that occurs exclusively in testing, or otherwise cannot be resolved I would suggest looking at your driver configuration and insure that the dialog you're seeing wouldn't be covered by the Driver setting for Unexpected_Alert_Behavior. If you've got that turned off, I'd try turning it on and see how it impacts your behavior.
I'm not entirely certain that will solve the listed problem bc the dialog you mention is part of the dom and the 'unexpected' alert behavior is typically not visible in that context from what I've seen. I believe this is also very specific to the IE implementation.
Finally, I think I would resort to using a method in my class to perform all findbys for me. In that method, I would use an explicit wait to check for the random addition and have it fall-through to resolve the actual requested object if the wait fails to resolve the 'error reference'.
/** By identifier for the error dialog addition.*/
private static final By ERR_DLG = By.className("error");
/**
* Delegate method that will attempt to resolve the occasional page error condition prior to performing the delegate lookup on the WebDriver.
* <p/>
* This method will fail on Assert behavior if the error dialog is present on the page.
*
* @param driver WebDriver referenced for the test instance.
* @param lookup By reference to resolve the desired DOM reference.
* @return WebElement of the specified DOM.
*/
private final WebElement safeResolve(WebDriver driver, By lookup) {
WebDriverWait wait = new WebDriverWait(driver, 1);
WebElement errDlgRef = null;
try {
errDlgRef = wait.until(ExpectedConditions.visibilityOfElementLocated(ERR_DLG));
} catch (TimeoutException te) {
//This is actually OK, in that in the majority of cases this is the expected behavior.
//Granted this is bad form for Unit-Testing, but Selenium at an Integration-Test level changes the rules.
}
Assert.assertNull("Unexpected Error Dialog exists in DOM", errDlgRef);
return driver.findElement(lookup);
}
This solution is kind of a hammer, but I think it would work. All lookups would need to go through this method. You'd probably also need an analogous method if you use the List WebDriver.findElements(By) function in your test, or a way to abstract it from this one.