I would suggest to implement a custom ExpectedCondition
, like:
import org.openqa.selenium.support.ui.ExpectedCondition
public static ExpectedCondition<Boolean> elementToBeVisibleWithRefreshPage(By element, int waitAfterPageRefreshSec) {
return new ExpectedCondition<Boolean>() {
private boolean isLoaded = false;
@Override
public Boolean apply(WebDriver driver) {
List elements = driver.findElements(element);
if(!elements.isEmpty()) {
isLoaded = elements.get(0).isDisplayed();
}
if(!isLoaded) {
driver.navigate().refresh();
// some sleep after page refresh
Thread.sleep(waitAfterPageRefreshSec * 1000);
}
return isLoaded;
}
};
}
Usage:
By element = By.id("xxx");
new WebDriverWait(driver, Duration.ofSeconds(30)).until(elementToBeVisibleWithRefreshPage(element, 10));
This will wait for 30 sec, until the element will be visible and will refresh the page with 10 sec pause, if the element wasn't visible.
Potentially sleep might be replaced with some other WebDriver wait, but this should also work.