Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does WebDriverWait override ImplicitlyWait when both are used?

I am using python+selenium webdriver for my automation. I have made use of ImplicitlyWait along with WebDriverWait.

Questions:

  1. Is it a good practice to use both ImplicitlyWait and WebDriverWait in a single script?

  2. 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.

like image 350
Praveen Pandey Avatar asked Jul 24 '13 09:07

Praveen Pandey


1 Answers

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:

  1. Run the WebDriverWait initially
  2. It hits the .FindElement call
  3. Selenium runs this, internally, for 10 seconds
  4. After the initial 10 seconds, your WebDriverWait would get a response.
  5. If the element is not found, it is run from step 1 again.
  6. 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.

like image 123
Arran Avatar answered Oct 13 '22 13:10

Arran