I am using:
driver.manage().timeouts().implicitlyWait(180, TimeUnit.SECONDS);
But it still fails continuously for the below element
driver.findElement(By.id("name")).clear(); driver.findElement(By.id("name")).sendKeys("Create_title_01");
I have added wait code:
for (int second = 0;; second++) { if (second >= 120) fail("timeout"); try { if (isElementPresent(By.id("name"))) break; } catch (Exception e) {} Thread.sleep(1000); }
Shouldn't implicit wait take care of waiting till an element is found? Also would it be better if I use Explicit wait instead of the code I have added that has Thread.sleep()
?
Explicit wait is more intelligent, but can only be applied for specified elements. However, it is an improvement on implicit wait since it allows the program to pause for dynamically loaded Ajax elements. In order to declare explicit wait, one has to use “ExpectedConditions”.
Explicit waits are used to halt the execution until the time a particular condition is met or the maximum time has elapsed. Unlike Implicit waits, Explicit waits are applied for a particular instance only.
You should choose to use Explicit or Implicit Waits. WARNING: Do not mix implicit and explicit waits. Doing so can cause unpredictable wait times. For example, setting an implicit wait of 10 seconds and an explicit wait of 15 seconds, could cause a timeout to occur after 20 seconds.
Implicit wait is used in the program when you are sure about the time taken by all web-elements on the web-page to load/visible and for certain Web-elements which you find time as one the varying factor in it's loading then you can use explicit wait.
TL;DR: Always use explicit wait. Forget that implicit wait exists.
Here is a quick rundown on the differences between explicit and implicit wait:
Explicit wait:
Implicit wait:
Code examples with explanation. First implicit wait:
WebDriver driver = new FirefoxDriver(); driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS); driver.get("http://somedomain/url_that_delays_loading"); WebElement myDynamicElement = driver.findElement(By.id("myDynamicElement"));
Now explicit wait:
WebDriver driver = new FirefoxDriver(); driver.get("http://somedomain/url_that_delays_loading"); WebDriverWait wait = new WebDriverWait(driver, 10); WebElement myDynamicElement = wait.until( ExpectedConditions.presenceOfElementLocated(By.id("myDynamicElement")));
Both code examples do the same thing. Find a certain element and give up if not found after 10 seconds. The implicit wait can do only this. It can only try to find an element with a timeout. The strength of explicit wait is that it can wait for all kinds of conditions. Also customize timeout and ignore certain exceptions.
Example of possible conditions: elementToBeClickable
, numberOfElementsToBeMoreThan
or invisibilityOf
. Here is a list of the built in expected conditions: https://seleniumhq.github.io/selenium/docs/api/java/org/openqa/selenium/support/ui/ExpectedConditions.html
More explanations:
The implicit wait timeout has effect only on findElement*
methods. If set then all findElement*
will "wait" for the set time before declaring that the element cannot be found.
How a findElement*
will wait is not defined. It depends on browser or operating system or version of selenium. Possible implementations are:
This list is gathered from observations and reading bug reports and cursory reading of selenium source code.
My conclusion: Implicit wait is bad. The capabilities are limited. The behaviour is undocumented and implementation dependent.
Explicit wait can do everything implicit wait can and more. The only disadvantage of explicit wait is a bit more verbose code. But that verbosity makes the code explicit. And explicit is better that implicit. Right?
Further reading:
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With