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()
?
Implicit waits are used to provide a waiting time (say 30 seconds) between each consecutive test steps across the entire test script or program. Next step only executed when the 30 Seconds (or whatever time is given is elapsed) after execution of previous step
Syntax:
Explicit waits are used to halt the execution till the time a particular condition is met or the maximum time which is defined, has elapsed. Implicit wait has applied between each consecutive test steps across the entire test script or programs while Explicit waits are applied for a particular instance only.
Syntax:
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:
Let's see the difference between explicit wait and implicit wait in the actual source code of selenium. I copied the code from the python binding of selenium because python is "easy to read".
The code of
WebDriverWait.until()
(explicit wait):Now in human language: explicit wait expects a method which returns a truish value if successful. It then repeatedly executes the given method with a delay in between. Expected errors from the given method are supressed. If the given method returns a truish value then explicit wait will return that value. If the time runs out a timeout exception is raised.
Compare to the code of
WebDriver.implicitly_wait()
(comments removed for brevity):self.execute()
isWebDriver.execute()
which callsRemoteConnection.execute()
which in turn does, as far as I can tell, an RPC to the remote side of selenium.In human language: the implicit wait sends a message to the "remote side" of the selenium webdriver. The remote side of the selenium webdriver is the part of selenium which is actually controlling the browser. What does the remote side do with the message? "It depends". It depends on the operating system and on the browser and on the version of selenium. As far as I can tell there is no guarantee about the actual behaviour of a specific implementation.
Possible implementations are:
Note that implicit wait only takes effect on find element(s) methods.
I have not looked up the actual source code of the remote sides of selenium. The information is gathered from reading the comments in the bug reports about implicit and explicit wait in selenium:
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 overhead because of multiple remote procedure calls. Also the explicit wait is a bit more verbose. But that verbosity makes the code explicit. And explicit is better that implicit. Right?
Further reading:
Have you tried using 'WebDriverWait' ? I imagine what you want is this:
This pretty much will, to my understanding, do what your current code is. It will constantly try the method (while ignoring not found exceptions) until the timeout of the passed in timespan is reached and a third parameter can be entered to specify the sleep in milliseconds. Sorry if this is what implicitlyWait does too!
Edit: I did some reading today and understand your question better and realise that this does exactly what your setting of implicit wait should do. Will leave it here just in case the code itself can help someone else.
Have you tried fluentWait? An implementation of the Wait interface that may have its timeout and polling interval configured on the fly. Each FluentWait instance defines the maximum amount of time to wait for a condition, as well as the frequency with which to check the condition. Furthermore, the user may configure the wait to ignore specific types of exceptions whilst waiting, such as NoSuchElementExceptions when searching for an element on the page.
see this link fluent wait description
In particular I used fluent wait in this way:
As you've noticed fluent wait returns found web element. So you simply pass the locator with By type and then you can perform any actions on the found web element.
Hope this helps you)
ImplicitWait :
Explicit Wait:
Implicit wait - It's global setting applicable for all elements and if element appear before specified time than script will start executing otherwise script will throw
NoSuchElementException
. Best way to use in setup method. Only affectBy.findelement()
.Thread.sleep()
- It will sleep time for script, not good way to use in script as it's sleep without condition. What if 2 seconds are not enough in 5% of the cases?Explicit wait: Wait for specify contains/attribute change. More used when application gives AJAX call to system and gets dynamic data and render on UI. In this case
WebDriverWait
is suitable.