硒:间歇性“未找到元素”问题

发布于 2024-11-07 03:24:05 字数 304 浏览 0 评论 0原文

我的硒测试时不时地会随机失败,并出现“未找到元素 X”的错误消息。我通过 ID 进行简单的选择,例如。

click('sideBar_queryButton')

当我使用dom检查器时,该元素就在那里,所以我想知道为什么selenium有时找不到它。当我再次运行相同的测试时,它会起作用或再次损坏,似乎没有可靠的方法来重现它。即使是存在多年的测试似乎也会随机中断,然后又神奇地再次发挥作用。插入一些睡眠语句有时会有所帮助,但并不可靠。所以我想知道我是否使用错误。还有其他人遇到过硒的这些问题吗?如果有,您是如何解决这些问题的?

Every now and then my selenium tests randomly fail with an "element X not found" error message. I do a simple select by ID, eg.

click('sideBar_queryButton')

When I use the dom inspector, the element is there, so I wonder why selenium doesn't find it sometimes. When I run the same test again, it works or it breaks again, there seems to be no reliable way of reproducing it. Even tests which are there for ages seem to randomly break and then magically work again. Inserting a few sleep statements sometimes helps but not reliably. So I wonder if I'm using it incorrectly. Has anyone else had these problems with selenium and if so, how did you fix them?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

无所谓啦 2024-11-14 03:24:05

编辑:我发现在我的页面中放置一些测试标记并等待它们出现要可靠得多。如果您使用可能在测试中创建竞争条件的异步操作,那么在完成操作后将测试标记插入到 html 中对我来说效果很好。例如

$('<div>').addClass("testMarker").append("OpXYZFinished").appendTo($('#content'));

,这样,您可以执行一个简单的“waitForTextPresent”来查看事情是否成功,并且它比猜测浏览器的加载状态可靠得多。 testmarker 类需要以用户不可见的方式进行格式化(例如字体颜色==背景颜色)。

感谢您的所有评论。在网上进行了一些更深入的挖掘之后
在我们的测试中,我终于发现结合这些语句而不是
简单的 waitForPageToLoad 可以解决我们的问题:

waitForPageToLoad('')
// 等待所有 ajax 活动停止。该检查是 jQuery 的 $.active
waitForCondition('selenium.browserbot.getUserWindow().$.active == 0', 5000)
// 等待所有 JS 正确初始化
暂停(1000)

那里仍然有一个停顿,有点难看,但确实如此
诀窍。

Edit: I found it to be much more reliable to put some test markers in my pages and wait for them to appear. If you use asynchronous operations which might create race conditions in your tests, inserting a test marker into your html after you finish the operation worked pretty well for me. E.g.

$('<div>').addClass("testMarker").append("OpXYZFinished").appendTo($('#content'));

That way, you can do a simple "waitForTextPresent" to see if things worked out and its much more reliable than guessing the browser's loading state. The testmarker class needs to be formatted in a way that it is not visible to the user (e.g. font color == background color).

Thanks for all your comments. After some deeper digging on the net and
in our tests I finally found combining these statements instead of a
simple waitForPageToLoad to be the cure for our issues:

waitForPageToLoad('')
// wait until all ajax activity has ceased. That check's jQuery's $.active
waitForCondition('selenium.browserbot.getUserWindow().$.active == 0', 5000)
// wait a second for all JS to properly initialize
pause(1000)

There is still a pause in there which is somewhat ugly, but it does
the trick.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文