Reputation: 759
Explicit wait is not working if the element located is of 'text'. But it is working fine if the driver performs some action i.e, entering text into text box or clicking a webelement etc.
public boolean waitForPageToLoad(String timeOutInSeconds) throws ScreenShotException, InterruptedException {
boolean bFlag = false;
WebElement element;
boolean bStatus = true;
int timeinseconds1 = Integer.parseInt(timeOutInSeconds);
try {
WebDriverWait wait = new WebDriverWait(webDriver, timeinseconds1);
while(timeinseconds1 > 0) {
element = wait.until(ExpectedConditions.visibilityOfElementLocated(By.id("myDynamicElement")));
Log.info("Element status at runtime -->"+element.isDisplayed());
if(!element.isDisplayed()) {
timeinseconds1 = timeinseconds1 - 500;
Thread.sleep(1000);
}
else {
System.out.println("working");
bFlag = bStatus;
Log.info("Element status: - >"+bFlag);
break;
}
}
}
catch (Exception e) {
screenShot.screenShot(e);
}
return bFlag;
}
The above code doesnt work if my locator is of text i.e, say if I want to check whether 'Title' of the question in the stackoverflow is visible or not within 40seconds.Driver will wait for 40seconds though the title appears less than that. But, it works fine if the locator is Title text box. Please let me know how to resolve this.
Upvotes: 0
Views: 679
Reputation: 2210
You are using the wait quite differently that it is supposed to be used.
try {
WebDriverWait wait = new WebDriverWait(webDriver, timeinseconds1);
element = wait.until(ExpectedConditions.visibilityOfElementLocated(By.id("myDynamicElement")));
} catch (TimeOutException toe) {
//handle the page not loading
}
//from now on continue the code as synchronous knowing that the page is loaded
Upvotes: 1