为什么Selenium不能找到动态添加的DOM元素?

时间:2022-10-06 00:03:22

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?

我使用jQuery添加了一个DOM元素(链接)。我可以看到Firebug中的元素,但是由于某种原因Selenium无法找到它。这是怎么回事?

Note: there is no AJAX involved, so the DOM element is added almost immediately.

注意:不涉及AJAX,因此几乎立即添加DOM元素。

== EDIT ==

= = = =进行编辑

Here's some code (using jQuery) that appends a link to the end of the document:

下面是一些代码(使用jQuery),它将链接附加到文档末尾:

$element = $("<a id="foo" href="#"></a>");
$element.appendTo($("body"));

I'm using Capybara (with Selenium) to find the link and click it, like so:

我使用Capybara(和Selenium)找到链接并点击它,就像这样:

find("#foo").click

I was having no problems with Capybara or Selenium until I started adding elements to the DOM with jQuery.

在使用jQuery向DOM添加元素之前,我对Capybara或Selenium没有问题。

1 个解决方案

#1


6  

Try to use waitForElementPresent in your test code.

尝试在测试代码中使用waitForElementPresent。

#1


6  

Try to use waitForElementPresent in your test code.

尝试在测试代码中使用waitForElementPresent。