简体   繁体   中英

Ruby unable to catch Selenium::WebDriver::Error::StaleElementReferenceError

I have created a method that is called for each web element accessed by the scripts, as to avoid the "StaleElementReferenceError" thrown by selenium. This is how the code looks:

     def reach_element(page,element)
       begin
         element.wait_until_present
       rescue Selenium::WebDriver::Error::StaleElementReferenceError
         puts '* retrying to reach element'
         page.refresh
         retry
       end
     end

It appears that the StaleElementReferenceError is ignored and the tests keep failing with this error. Am I doing something wrong?

CORRECTIONS:

This error shouldn't appear at all for it to be rescued by ruby. The main cause was the old version of watir-webdriver gem. If you still encounter this error, a simple gem update should do the trick.

We mostly got rid of stale element issues for when you take an action on an element in watir-webdriver last year. This is the code: https://github.com/watir/watir-webdriver/blob/master/lib/watir-webdriver/elements/element.rb#L597

When an action is taken on the element, but it is stale, it will re-look it up with the selector provided. It will fail for not existing if it isn't there.

Are you seeing your element go stale between when you locate it, but before it becomes visible? That's an interesting use case that I have plans to fix. If that is your issue, refreshing the page will force the element to go stale, so that will just repeat your issue. Remove the refresh, and it should keep relocating the stale element until it is present.

If that isn't the issue or that doesn't work, provide a stack trace of what you are seeing.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM