The Issue

You’ve used every Watir, Selenium Ruby bindings, etc. waiting technique you can think of and they just don’t seem to work.  The solution: an explicit wait using a loop.

A Solution

Here is a method you can try.  It’s advantages are that it is fully visible and bypasses almost all of the built-in timers and waits in Selenium, the Ruby bindings, and Watir.

Parameters:

  • How  –  a string or symbol indicating the element’s identifying attribute
  • What  –  a string that the identifying attribute must contain.
  • Wait  –  the number of seconds to try before returning false
  • Interval  –  the fraction of a second to wait before the next try

‘how’ is forced to be a symbol that selenium will recognize as an element attribute or property.

‘what’ is converted to a regular expression so it doesn’t have to equal the entire attribute value.  It must be a value unique on the page for the attribute.

Examples:

 

This call tries every half second for 10 seconds for element with :class containing ‘unique class-on-page’ to exist.

Remember that ‘exists? ‘ only means the element is present in the HTML.  It is ‘present?’ only when it is also ‘visible?’.   And a ‘visible?’ or ‘present?’ element can be ‘enabled?’ or ‘disabled?’, i.e., clickable or not.

This method presents an explicit wait that is isolated from all of the timeouts in the Watir, Ruby bindings, Selenium, browser driver stack, with one exception.  That is the browser driver implicit wait (set by @browser.driver.manage.timeouts.implicit_wait = n) which is defaulted to 0.   Warning: Don’t use implicit wait, especially with explicit waits (even this method).  Behaviour is unpredictable because the implicit wait is invoked by the browser driver in its execution of the ‘exists?’ call and may throw off the timing of other waits.  

The Result

Explicit Waits using a loop is a way of waiting for an element to appear that is isolated from all but one of the timeouts and waits in the Watir, Ruby bindings, Selenium, browser driver stack, and is completely exposed to ease debugging.

Take a look at How To Use Explicit Waits on Browsers with Selenium & Watir Webdriver for more about the Watir and Selenium Webdriver wait implementations.