Reputation: 737
This below WebElement is not present in WebPage. However I want to know about Fluent wait.
Here when webdriver trying to identify BE_flight_flseah_btn
, its supposed to wait max 5 mins based on code,but it throws error within seconds saying no such element.
Wait<WebDriver> w=new FluentWait<WebDriver>(dr)
.withTimeout(5,TimeUnit.MINUTES)
.pollingEvery(5,TimeUnit.MILLISECONDS)
.ignoring(NoSuchElementException.class);
WebElement we=w.until(new Function<WebDriver,WebElement>()
{
public WebElement apply(WebDriver dr)
{
return dr.findElement(By.id("BE_flight_flseah_btn"));
}
});
why it is not waiting for 5 mins, anything wrong in this code? Please help me on this.
Upvotes: 2
Views: 5144
Reputation: 69
.ignoring(NoSuchElementException.class) - This should be imported from import org.openqa.selenium.NoSuchElementException;
By default Eclipse ask to import NoSuchElementException class either from java.util.NoSuchElementException; or org.openqa.selenium.NoSuchElementException;
If we select - java.util.NoSuchElementException; then program will not wait for maximum time as mentioned in the polling statement.
Technically, nothing is wrong with the code, it is just wrong package is imported
Upvotes: 0
Reputation: 686
public WebElement waitForElementDisplayed(final Supplier<By> by,
final int timeToWaitInSec) {
Wait<Browser> wait = new FluentWait<>(this)
.withTimeout(timeToWaitInSec, TimeUnit.SECONDS)
.pollingEvery(5, TimeUnit.MILLISECONDS)
.ignoring(WebDriverException.class);
wait.until(ExpectedConditions.presenceOfElementLocated(by.get()));
WebElement foo = wait.until((Function<Browser, WebElement>) (Browser driver) -> {
try {
Element element = driver.findElement(by);
if (element.isDisplayed()) {
return element;
}
} catch (Exception e) {
}
return null;
});
return foo;
}
Invoke this method - your script should be working.
Upvotes: 0
Reputation: 5818
Make sure you have imported the correct NoSuchElementException
class.
import org.openqa.selenium.NoSuchElementException;
Because if you use any ide like eclipse or Netbeans the auto import will most probably importing import java.util.NoSuchElementException;
which is a wrong one
You are polling every 5 milliseconds and waiting for 5 minutes. So it will check 60000 times to find the element.It will affect the performance.Either increase the polling TimeUnit to Seconds or Reduce the Timeout to Seconds
Fluent Wait: Let’s say you have an element which sometime appears in just 1 second and some time it takes minutes to appear. In that case it is better to use fluent wait, as this will try to find element again and again until it find it or until the final timer runs out.
Explicit Wait: There can be instance when a particular element takes more than a minute to load. In that case you definitely not like to set a huge time to Implicit wait, as if you do this your browser will going to wait for the same time for every element.
To avoid that situation you can simply put a separate time on the required element only. By following this your browser implicit wait time would be short for every element and it would be large for specific element.
For your requirement instead of FluentWait and Wait you can use WebDriverWait along with ExpectedConditions though WebDriverWait is an extension of FluentWait.
You can read about the comparison over this link
Your Condition can be handled as
//timeunit in seconds.
//There are two other constructors available you can see that in the links
WebDriverWait wait = new WebDriverWait(driver, 300);
WebElement element = wait.until(ExpectedConditions.presenceOfElementLocated(By.id("BE_flight_flseah_btn")));
Upvotes: 5