Have you considered the possibility that you may be mistakenly testing for presenceOfElementLocated rather than visibilityOfElementLocated? The visibilityOfElementLocated function only works if the element being searched for has its display CSS attribute set. Some individuals try to determine if an element exists by using ".findElements(By).size() > 0", but here is an alternative approach that involves exception handling.
While it may seem excessive, one option is to create your own visibility function. In this instance, I have named it "elementExists()". This function checks for the presence of an element and then manages any visibility-related exceptions that may arise:
public boolean elementExists( By locator ) {
WebElement foo = null;
try {
foo = this.getElementByLocator( locator, 10 );
} catch ( TimeoutException te) {
System.out.println("A timeout occurred while searching for the element: " +
locator.toString() );
//Swallow exception: ExceptionUtils.getMessage(te);
return false;
} catch ( ElementNotVisibleException env ) {
System.out.println("The element was found, but it is not visible: " +
locator.toString() );
//Swallow exception: ExceptionUtils.getMessage(env);
return false;
}
if ( foo == null ) {
return false;
} else {
return true;
}
}
public WebElement getElementByLocator( By locator, int timeout ) {
System.out.println("Calling method getElementByLocator: " +
locator.toString() );
int interval = 5;
if ( timeout <= 20 ) interval = 3;
if ( timeout <= 10 ) interval = 2;
if ( timeout <= 4 ) interval = 1;
Wait<WebDriver> wait = new FluentWait<WebDriver>( se.myDriver )
.withTimeout(timeout, TimeUnit.SECONDS)
.pollingEvery(interval, TimeUnit.SECONDS)
.ignoring( NoSuchElementException.class,
StaleElementReferenceException.class );
WebElement we = wait.until( ExpectedConditions
.presenceOfElementLocated( locator ) );
return we;
}