Reputation: 11
Can a condition - examples of click on a button / driver.navigate.back() - be included on expiration of time units (timeout of WebDriverWait)?
Example: For the below statements - WebDriverWait wait60 = new WebDriverWait(driver, 60); wait60.until(ExpectedConditions.elementToBeClickable(driver.findElement(By.locator)));
If the elementtobeclickable is not loaded on the webpage within the 60 seconds - I would like the statement driver.navigate.back() be executed. Any means of defining this type of statement (say at class level) so all wait60.until conditions lead to the same defined statement(s) on timeout?
Upvotes: 1
Views: 1049
Reputation: 4424
Put it in a try-catch block like this:
try{
WebDriverWait wait60 = new WebDriverWait(driver, 60);
wait60.until(ExpectedConditions.elementToBeClickable(driver.findElement(By.locator)));
}catch(Exception e){
System.out.println("Element not found in due time. "+e.getMessage);
driver.navigate().back();
}
In case the element is not found in due time, an TimeoutException will be thrown and it will be subsequently handled in catch block. Also, after that the browser is navigated back.
Upvotes: 0
Reputation: 21
I think this is not included in the WebDriverWait
functionality. I would probably use try-catch exception handling, and when you catch timeoutException
, then call driver.navigate.back()
You can create a method that would do this and call it whenever you want.
Upvotes: 2