硒:间歇性“未找到元素”问题
我的硒测试时不时地会随机失败,并出现“未找到元素 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
编辑:我发现在我的页面中放置一些测试标记并等待它们出现要可靠得多。如果您使用可能在测试中创建竞争条件的异步操作,那么在完成操作后将测试标记插入到 html 中对我来说效果很好。例如
,这样,您可以执行一个简单的“waitForTextPresent”来查看事情是否成功,并且它比猜测浏览器的加载状态可靠得多。 testmarker 类需要以用户不可见的方式进行格式化(例如字体颜色==背景颜色)。
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.
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).