Java Selenium WebDriverWait waits longer than specified wait

309 views
Skip to first unread message

Eitel Dagnin

unread,
Jul 19, 2021, 1:50:20 PM7/19/21
to Selenium Users
Disclosure: Posted on SO as well over here: 


I don't understand what's happening with the newer version of Selenium's WebDriverWait but as far as I can see, it's not behaving the way I'd expect it to.

I have tried all the versions from 4.0.0-beta-1 - 4.0.0-beta-4 and I have just updated to 4.0.0-beta-4 as my preferred version.

I use these methods in my own functions:

  1. isPresent
  2. isVisible
  3. isClickable

Each of them have their respective constructors that they call:

  1. WebDriverWait Presence = new WebDriverWait(webDriver, Duration.ofSeconds(1L)); 
  2. WebDriverWait Visible = new WebDriverWait(webDriver, Duration.ofMillis(1L));
  3. WebDriverWait Clickable = new WebDriverWait(webDriver, Duration.ofMillis(1L));

A couple of things that I found:

  1. The old constructors would accept their time durations provided as type Long and in seconds
  2. The new constructors accept their time durations provided as type Duration
  3. Duration has it's own methods which accept type Long

Previously, you could write it like this:

  • WebDriverWait Presence = new WebDriverWait(webDriver, 1L);

Now, it looks like this (as shown above):

  • WebDriverWait Presence = new WebDriverWait(webDriver, Duration.ofSeconds(1L));

The only difference being how you define the wait.

With using Duration, you can specify the "type":

  1. Duration.ofDays(1L)
  2. Duration.ofHours(1L)
  3. Duration.ofMinutes(1L)
  4. Duration.ofSeconds(1L)
  5. Duration.ofMillis(1L)
  6. Duration.ofNanos(1L)

In my above code example, I define the WebDriverWait using the Duration.ofSeconds(1L), meaning it should wait for just 1 second, however, it waits for 10 seconds instead.

In fact, it waits for 10 seconds whether it's set to Duration.ofMillis or Duration.ofNanos too.

So, either I really don't understand the implementation correctly, or there's something I am missing, or there's an issue with the actual WebDriverWait class.

It's also worth mentioning, it doesn't wait 10 seconds whether the WebElement exists or not, it only waits the full duration if the WebElement does NOT exist.

Hoping someone can assist with this.

joseph...@gmail.com

unread,
Jul 26, 2021, 4:49:19 PM7/26/21
to Selenium Users
It sounds like you have an implicit wait defined on the WebDriver object that is waiting for 10 seconds when the WebElement does not exist.  Once that implicit wait times out, it then tries the explicit wait you have defined in your WebDriverWait object, which times out in the amount of time you have specified.

This is why it is recommended to not mix implicit and explicit waiting strategies, as there can be unexpected results when using both.  Pick one or the other and stick with it.  I prefer explicit waits since they allow a lot more flexibility.  You are already showing you have an understanding of them in your code above.
Reply all
Reply to author
Forward
0 new messages