I am using python+selenium webdriver for my automation. I have made use of ImplicitlyWait along with WebDriverWait.
Questions:
Is it a good practice to use both ImplicitlyWait and WebDriverWait in a single script?
Suppose my ImplicitlyWait value is 20 and WebDriverWait value is 10 seconds. Will WebDriverWait override 20 when it waits for a specific element? What happens when ImplicitlyWait value is less than WebDriverWait?
Please suggest. I tried finding this answer on the internet but did not get any full proof or convincing answer.
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:
.FindElement
callWebDriverWait
would get a response.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.
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