Search code examples
capybara-webkit

capybara: mysterious 'element at X no longer present in the DOM'


I have written cucumber test scenario's that are tested with webkit and a firefox driver. In firefox the tests all pass, but with webkit one of them fails with error

  Element at 40 no longer present in the DOM (Capybara::Webkit::NodeNotAttachedError)
  /home/r/project/share/support/actions.rb:64:in `block (2 levels) in follow_link_in_new_tab'
 /home/r/project/duplo/share/support/actions.rb:10:in `with_scope'................

This happens during the execution of a routine that is triggered by test step 'When I follow "Linkname". Strangely enough, most of the times this code works perfect, but in one case I get this 'no longer present error'.

The routine performs this:

res = find( 'a', text: text, visible: true)
if ((res[:target] || '') == '_blank')
    @win = window_opened_by { res.click }
else
    res.click
end

I found out that if I change this to

find( 'a', text: text, visible: true).click

the message disappears and the scenario passes the test. Who can help me understanding what can be the problem here. Why is this failing when the find result is assigned to a variable and why is it only failing in only few situations?

I use ruby 2.4.0 and capybara-webkit 1.2.0

thanks, Ruud


Solution

  • You don't indicate exactly which line is generating the error (which one is line 64), however the error indicates that you're still using an element after it has been removed from the page, either by JS deleting the element or a new page being loaded.

    Additionally the visible: true option really shouldn't be needed since that is normally the default value for the visible option (unless you've changed it which is a terrible idea when testing software, not as bad if just scraping sites)

    Also - capybara-webkit 1.2.0 was released in July 2014 -- You REALLY want to update that to the latest release if you're testing anything even slightly modern, as well as probably updating your ruby to at least the latest 2.4.x release.