Sometimes implicit wait fails, saying that an element exists but it really doesn't.
The solution is to avoid using driver.findElement and to replace it with a custom method that uses an Explicit Wait implicitly. For example:
import org.openqa.selenium.NoSuchElementException;
public WebElement element(By locator){
Integer timeoutLimitSeconds = 20;
WebDriverWait wait = new WebDriverWait(driver, timeoutLimitSeconds);
try {
wait.until(ExpectedConditions.presenceOfElementLocated(locator));
}
catch(TimeoutException e){
throw new NoSuchElementException(locator.toString());
}
WebElement element = driver.findElement(locator);
return element;
}
There are additional reasons to avoid implicit wait other than sporadic, occasional failures (see this link).
You can use this "element" method in the same way as driver.findElement. For Example:
driver.get("http://yoursite.html");
element(By.cssSelector("h1.logo")).click();
If you really want to just wait a few seconds for troubleshooting or some other rare occasion, you can create a pause method similar to what selenium IDE offers:
public void pause(Integer milliseconds){
try {
TimeUnit.MILLISECONDS.sleep(milliseconds);
} catch (InterruptedException e) {
e.printStackTrace();
}
}