Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why can't Selenium find dynamically added DOM elements?

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.

like image 770
Matt Fichman Avatar asked Dec 08 '10 00:12

Matt Fichman


Video Answer


1 Answers

Try to use waitForElementPresent in your test code.

like image 52
Jigar Shah Avatar answered Oct 23 '22 05:10

Jigar Shah