Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

StaleElementReferenceError with Selenium with content already loaded

I'm using Capybara with Ruby 1.9.3 using the selenium driver in order to get information off a website. After clicking through a couple of pages I visit the page I want and I put:

 all(:css, 'td').each { |td| a_info << td }
 a_info.each {|t| puts t.text }

The error I then get after about 10 seconds of waiting:

[remote server] resource://fxdriver/modules/web_element_cache.js:5628:in `unknown': Element not found in the cache - perhaps the page has changed since it was looked up (Selenium::WebDriver::Error::StaleElementReferenceError)

Followed a lot more remote server errors. I've given the page 10-30 seconds of sleep time and it's still not loading and when I do print page.html, I see a javascript script and then all the td's that I'm trying to get info from. I know that the error means an element being found is not the current one but it seems like all the elements have been loaded already so I'm not sure why they wouldn't exist anymore. I've scoured the internet for hours looking for this and would love any kind of help from possible solutions to try and the next steps for trying to figure it out. I can provide any extra information needed, just let me know.

like image 473
Anoel Avatar asked Aug 31 '12 17:08

Anoel


People also ask

What is stale exception in selenium?

A stale element reference exception is thrown in one of two cases, the first being more common than the second: The element has been deleted entirely. The element is no longer attached to the DOM.

Why do we get stale element exception?

The stale element reference error is a WebDriver error that occurs because the referenced web element is no longer attached to the DOM. Every DOM element is represented in WebDriver by a unique identifying reference, known as a web element.


2 Answers

It happens when you make a scope and into that you change the page, for example, but keeps making assertions inside it, like the following:

within 'selector' do
  expect(page).to have_link 'Link'

  visit 'page'

  expect(page).to have_link 'I do not know this whitin, I am stale.'

  find('I do not know this within, I am stale.').click
end

Just reopen you scope again over to keep working on the old one.

within 'selector' do
  expect(page).to have_link 'Link'
end

visit 'page'

within 'selector' do
  expect(page).to have_link 'Now i am fresh'

  find('#now-i-am-fresh').click
end
like image 176
Washington Botelho Avatar answered Jan 04 '23 13:01

Washington Botelho


This is my least favorite error. I'm gonna refer you to this exact question on stack overflow asked about a year before Random "Element is no longer attached to the DOM" StaleElementReferenceException

like image 21
Greg Avatar answered Jan 04 '23 13:01

Greg