For the first point, it's probably a personal choice. I don't use implicit waiting
at all simply because I like to have the control of where Selenium is waiting and where it is not. Setting the implicit wait
is blindly telling Selenium, if you don't find my element, then wait for a certain time until you can. No matter what it is, no matter the consequence, no matter what page, you wait until you either find it or 20 seconds has passed.
That's fine if that's the way you want to go, but for me, the problem comes from if my element is taking 30 seconds to appear on the page then that's a problem in itself anyway. Selenium just hides it, it can cover over the problem.
However, there are some times the element does take a while to appear and to be 'ready' and that's when explicit waiting
comes in. When you do expect it.
As for the waiting, what will happen is initially when your WebDriverWait
is hit, it will run and try to find the element. Selenium will see you have your implicit wait
set so it will constantly try to find that element for up to 20 seconds.
It will do this 'for free', behind the scenes.
Once that's expired, your WebDriverWait
will get a response and since your implicit wait
timeout is larger than your WebDriverWait
timeout, it will fall over with an exception (Timeout exception).
In terms of if the WebDriverWait
value is higher than the implicit wait
value (your example in the comments), then the process would be:
- Run the WebDriverWait initially
- It hits the
.FindElement
call
- Selenium runs this, internally, for 10 seconds
- After the initial 10 seconds, your
WebDriverWait
would get a response.
- If the element is not found, it is run from step 1 again.
- Once it gets to step 4, if the element is not found still, it will now throw a timeout exception.
To further explain (pseudo C# code):
driver.Manage().Timeouts().SetImplicitWait(TimeSpan.FromSeconds(10));
WebDriverWait wait = new WebDriverWait(driver, TimeSpan.FromSeconds(20));
wait.Until(w =>
{
return w.FindElement(By.Id("something")).Displayed;
}
We have a WebDriverWait
of 20 seconds, and an implicit wait
of 10 seconds.
It will hit:
return w.FindElement(By.Id("something")).Displayed;
for the first time. Selenium, internally, will run the .FindElement
for 10 seconds. After this 10 seconds has passed, my WebDriverWait
will then check it's condition (.Displayed
). If this is not met, then it the WebDriverWait
will run the same line of code again. Again, Selenium will do as it did before, and run for 10 seconds. Once it comes back from that, if the condition is not met this then means, the time the WebDriverWait
has been waiting is a total of 20 seconds and will then throw an error.