本文介绍了将隐式等待和显式等待结合在一起会导致意外的等待时间的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的两种情况-

1)首先

@driver.manage.timeouts.implicit_wait = 30
@wait = Selenium::WebDriver::Wait.new(:timeout => 45) # Time greater than implicit
@wait.until {@driver.find_element(:tag_name => "body").text.include?("hey")}

这使驾驶员有45秒的时间来搜索文本(这是预期的结果)

Which gives the driver 45 seconds to search for the text(which is expected)

2)秒

@driver.manage.timeouts.implicit_wait = 30
@wait = Selenium::WebDriver::Wait.new(:timeout => 5) # Time less than implicit
@wait.until {@driver.find_element(:tag_name => "body").text.include?("hey")}

这现在使驱动程序 30秒来搜索文本(不需要)

This now gives the driver 30 seconds to search for the text(not expected)

有没有办法让硒仅等待explicit等待时间,而不等待两者中的较大者?

Is there a way to make selenium wait only for the explicit wait time and not for the greater of the two?

注意-不声明隐式等待时间不是一种选择,因为每次驱动程序无法找到东西时,我都无法让硒挂起.

Note - Not declaring the implicit wait time is not an option, cause I cannot afford to let selenium hang each time the driver is unable to find something.

使用Selenium版本30,Windows,ff

Using Selenium version 30, windows, ff

推荐答案

不要混合使用隐式和显式等待.问题的部分原因是隐式等待通常(但可能并非总是如此!)在WebDriver系统的远程"侧实现.这意味着它们被嵌入"到IEDriverServer.exe,chromedriver.exe,安装在匿名Firefox配置文件中的WebDriver Firefox扩展以及Java远程WebDriver服务器(selenium-server-standalone.jar).显式等待专门在本地"语言绑定中实现.使用RemoteWebDriver时,事情变得更加复杂,因为您可能同时使用了系统的本地端和远程端.

Don't mix implicit and explicit waits. Part of the problem is that implicit waits are often (but may not always be!) implemented on the "remote" side of the WebDriver system. That means they're "baked in" to IEDriverServer.exe, chromedriver.exe, the WebDriver Firefox extension that gets installed into the anonymous Firefox profile, and the Java remote WebDriver server (selenium-server-standalone.jar). Explicit waits are implemented exclusively in the "local" language bindings. Things get much more complicated when using RemoteWebDriver, because you could be using both the local and remote sides of the system multiple times.

这是这样的工作方式:本地代码-> Java远程服务器->远程服务器上的本地Java语言绑定->远程"组件,例如Firefox扩展,chromedriver.exe或IEDriverServer.exe.在网格情况下,它甚至更加复杂,因为它们之间可能还会有其他跃点.

This is how that would work: local code -> Java remote server -> local Java language bindings on the remote server -> "remote" component like the Firefox extension, chromedriver.exe or IEDriverServer.exe. It's even more complex in the grid case, since there could be other hops in between.

因此,当您尝试混合使用隐式等待和显式等待时,就会迷失为未定义行为".您可能可以弄清楚该行为的规则是什么,但是随着驱动程序的实现细节的更改,它们可能会有所更改.所以不要这样做.

Thus, when you try to mix implicit and explicit waits, you've strayed into "undefined behavior". You might be able to figure out what the rules of that behavior are, but they'll be subject to change as the implementation details of the drivers change. So don't do it.

如果您不使用隐式等待,则在找不到元素时不应出现挂起"现象.驱动程序应立即引发NoSuchElement异常.

You shouldn't be experiencing "hangs" when an element can't be found if you're not using implicit waits. The driver should throw a NoSuchElement exception immediately.

这篇关于将隐式等待和显式等待结合在一起会导致意外的等待时间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-28 06:02