Telling Watir-WebDriver to always locate elements upon use

One of the differences between Watir and Watir-WebDriver is how elements are located. Watir-WebDriver was designe to store references to DOM elements previously used, and this can cause the stale ObseleteElementError exception, as I explained previously.

Watir-WebDriver 0.3.1 has recently been released, and now there is a configurable option to specify that you always want to create a fresh reference when using an element.

 Watir::always_locate = true b = 

This will result in a performance hit, but in my experience, it isn’t too bad. Jari Bakken ran WatirSpec using this config which resulted in a 10 second increase in running time (from 145 seconds to 155 seconds) which in my opinion is worth it to have more reliable test results.

Thanks to Jari for working on this. Hope you enjoy.

Update: 15 August 2011: I only just realized this is enabled by default, so you’ll get it automatically when you update your gem. Neat.

Author: Alister Scott

Alister is an Excellence Wrangler for Automattic.

7 thoughts on “Telling Watir-WebDriver to always locate elements upon use”

    1. Does this mean you got rid off all the ObsoleteElementError exceptions or that you’ve managed to decrease them dramatically? I still use some sleep time to avoid it being raised on pages where AJAX is excessively used. I’m not proud of it, since it introduces a lot of overhead, but it’s the only method that does the work for me ATM. :(

      Did you do some other changes to your tests besides updating to the latest gem?

      Thanks in advance


      1. Hi Alister,

        I have configured the stings as you mentioned. But still I am hitting upon this inconsistent issue frequently.

        Watir::always_locate = true b =

        Can you help out here.

        Thanks in Advance


  1. I recoded everything to work with the new element discovery. So I’m a bit miffed on a personal note. But good news all around, I suppose! I can never find any release notes for watir-webdriver; from whence do you source your info?


  2. I don’t know if this is related. I get a StaleElementException in very specific cases. For example, here is some logic I have:

    on LoginPage do |page|
    page.customerCode = user_type[0]
    page.loginName = user_type[1]
    page.password = user_type[2]

    if page.error_div.exists?
    if page.error.strip == “Invalid login name or password”
    # do stuff here

    The problem is with the page.error_div.exists?. Here’s the scenario.

    In the first run through, a user login is attempted and it fails. The check for the error_div works fine: the message is found and the # do stuff logic is executed.
    Another user login is then attempted with a different user. That user is able to login.
    However, when the logic to run the check for the error_div runs I get the StaleElementException.

    The problem is that the error (and thus the div object) is not appearing after the second login because I’m on a different page. So obviously that div won’t be there. But that should just mean the check fails and the script continues on, right?

    I did try your trick here with the “always_locate” but that didn’t seem to work for me either. (I’m using watir-webdriver 0.40.)

    Just given the logic I show above, is there a better way I could be doing this?


Comments are closed.