Why can't Selenium find dynamically added DOM elements?

capybara, javascript, jquery, selenium

Solution

Try to use waitForElementPresent in your test code.

Problem

I added a DOM element (a link) with jQuery. I can see the element in Firebug, but for some reason Selenium can't find it. What is going on here? Note: there is no AJAX involved, so the DOM element is added almost immediately. == EDIT == Here's some code (using jQuery) that appends a link to the end of the document: ``` $element = $("<a id="foo" href="#"></a>"); $element.appendTo($("body")); ``` I'm using Capybara (with Selenium) to find the link and click it, like so: ``` find("#foo").click ``` I was having no problems with Capybara or Selenium until I started adding elements to the DOM with jQuery.

Original source