Accidental Technologist

Musings about Entrepreneurship, Technology and Software Development

  • Home
  • About
  • Still River Software
  • Privacy Policy

Powered by Genesis

You are here: Home / Ruby on Rails / Fixing StaleElementReferenceError When Using Capybara

Fixing StaleElementReferenceError When Using Capybara

December 4, 2018 by Rob Bazinet Leave a Comment

Tweet

I’ve been spending an extended period of time lately writing Feature specs for a Ruby on Rails project using Capybara. Overall it’s been a fun project to work on but I had run into a few issues along the way. One in particular is the StaleElementReferenceError exception that get thrown when you are trying to find an element on your page that you know is there but Selenium can no longer find.

The error appears to be a timing issue between the page loaded in the browser, it being rendered and Capybara and Selenium trying to find the element. After much trial and error, along with many Google searches, I found no solutions that were reasonable. Even though many people had the same problem.

The solution was pretty simple actually. I added a rescue to my test and simply slept for 1 second and retry the failure. I admit, this seems like a hack but it works and that’s all I really cared about at the time. I see no ill effects from this, no measurable delay.

RSpec.feature "Listings", type: :feature, js: true do
  scenario "can edit common area - step 3" do
    begin
      post_a_space_step_1_with
      click_on 'Next'
      post_a_space_step_2_with
      click_on 'Next'
      click_on 'Edit Common Area'
      expect(page).to have_content "Step 1"
    rescue Selenium::WebDriver::Error::StaleElementReferenceError
      sleep 1
      retry
    end
  end
end

I’m certainly open to a better solution or an explanation as to why this might be bad. Please leave comments. Hopefully I help someone get around this really annoying problem.

Share this:

  • LinkedIn
  • Twitter
  • Facebook
  • Email
  • More
  • Pinterest
  • Tumblr
  • Pocket
  • Reddit

Related

Filed Under: Ruby on Rails Tagged With: capybara, Ruby on Rails, selenium

Care about your privacy? I do and use Fathom Analytics on this site.

Fathom Analytics

Recent Posts

  • Social Media Times Are Changing
  • It has certainly been a long time…
  • How to Fix Rails Flash Rendering When Using Hotwire
  • Hotwire Fix for CORS Error when using Omniauth
  • Fix Installation of Ruby using rbenv on macOS Big Sur

Categories

Services I Love

HatchBox - Easy Rails Deploys Fathom Analytics
Follow @rbazinet

Rob Bazinet
@rbazinet

  • Great little utility https://t.co/6AgboJv6oI
    about 21 hours ago
  • Funny watching the hate to show up in my time line for ChatGPT. People getting defensive about it.
    about 4 days ago
  • I just backed The SaaS Playbook, by Rob Walling on @Kickstarter https://t.co/1FpTWN9c9v
    about 4 days ago
  • This looks like an interesting project. I need to dig in a bit. https://t.co/gacEX9AmEn "Phlex — fast, object-orie… https://t.co/4AUL0Z0SBl
    about 5 days ago
  • I attempted to use ChatGPT today and asked a couple questions it couldn’t answer. I thought it was a magical black… https://t.co/uJGw0CgUAv
    about 5 days ago
  • RSS - Posts
  • RSS - Comments
Find me on Mastodon