Selenium 2.0rc3 点击功能太快?
首先,对于这个神秘的问题感到抱歉。
我的团队目前正在使用 Selenium 2.0rc3(带有 python)通过 chrome 测试我们的 Web 应用程序。当我们使用 Selenium 2.02b 版本时,我们的测试通过了(速度有点慢,而且我们在 webdriver 中添加了一些小技巧)。升级后,测试变得非常快并且开始失败。经过调试,我们发现大多数测试失败,因为 webdrivers click() 函数没有阻塞() 连续调用。目前,我们在每次点击后添加了 0.5 秒的 sleep()/超时,虽然这解决了眼前的问题,但它并没有完全实现我们的主要目标(即加快测试速度)
First off, sorry for the cryptic question.
My team is currently using Selenium 2.0rc3 (with python) for testing our web app with chrome. When we used the 2.02b version of Selenium, our test passed (it was a little slow and we had small hacks that we added to webdriver). After we upgraded, the test became extremely fast and started failing. After debugging we found out most test failed because webdrivers click() function was not blocking() successive calls. Currently we added a sleep()/timeout of .5 secs after each click and while this solves the immediate problem, it doesn't quite achieve our main goal (which is to speed up our test)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您的问题并不是点击得太快。只是它在该元素出现之前点击。有两种方法可以解决这个问题:
恐怕我还没有使用 WebDriver Python 绑定。不过,我可以告诉您它是如何在 Java 中完成的,并希望您能自己找到等效的 Python 版本。
为了等待一个元素,我们在 Java 中有一个名为
WebDriverWait
的类。您可以编写一个Function
并将其传递给until()
方法,该方法仅在元素存在时传递。实现此目的的一种方法是使用driver.findElements( By... )
或将driver.findElement( By... )
包装在异常处理程序中。轮询Function
直到返回 true 或达到指定的超时。第二种方法是适合您情况的首选方法,在 Java 中您可以执行
driver.manage().timeouts().implicitlyWait( ... )
。Your problem is not really that it's clicking too fast. Just that it's clicking before that element is present. There are two ways to get round this:
I'm afraid I haven't used the WebDriver Python bindings. However, I can tell you how it is done in Java and hopefully you can find the Python equivalent yourself.
To wait for an element, we have in Java a class called
WebDriverWait
. You would write aFunction
which you pass to theuntil()
method which passes only when the element exists. One way you could do that is withdriver.findElements( By... )
or wrapdriver.findElement( By... )
in an exception handler. TheFunction
is polled till it returns true or the timeout specified is hit.The second method is the preferred method for your case and in Java you can do
driver.manage().timeouts().implicitlyWait( ... )
.我已经尝试过适用于 chrome 的 selenium-2 rc3 python 绑定。我的经历与您所描述的相反 - 单击后,驱动程序不知道页面已准备好继续。因此,他们并没有加快测试速度,反而变得非常慢(因为司机已经等待了很长时间)。然而,firefox 驱动程序似乎相当稳定 - 也许您应该坚持使用它,直到 chrome 驱动程序进一步完善。
I've tried the selenium-2 rc3 python bindings for chrome. My experience was the opposite of what you're describing - after clicking, the driver didn't know that the page was ready for it to continue. So instead of speeding up the tests, they turned out very slow (because the driver was waiting for ages). However, the firefox driver seems pretty stable - maybe you should stick with it until the chrome driver gets baked a bit more.
如果 click() 执行 ajax 调用,我建议您使用 NicelyResynchronizingAjaxController
If the click() executes ajax calls I would suggest you to use NicelyResynchronizingAjaxController