混合隐式和显式等待 [英] Mixing implicit and explicit waits
问题描述
文档说:
一个隐式等待是告诉WebDriver对DOM进行轮询 尝试查找一个或多个元素(如果存在)时的时间量 暂时无法使用.
An implicit wait is to tell WebDriver to poll the DOM for a certain amount of time when trying to find an element or elements if they are not immediately available.
子问题:
对于find_elements_by_
(复数),在继续执行脚本之前,implicit_wait
等待多少个元素存在?还是implicit_wait
仅适用于find_element_by_
(单数)?如果是这样,文档中或元素"是什么意思?
In the case of find_elements_by_
(plural), how many elements does implicit_wait
wait for to exist before continuing with your script? Or does implicit_wait
only work with find_element_by_
(singular)? If so what do the docs mean by "or elements"?
从一个SO答案中,我读到最好不要在同一脚本中同时使用隐式和显式等待.我注意到了,因为我希望测试尽可能强大.
From an SO answer I read that it's best not to use both implicit and explicit waits in the same script, which I took notice of as I'd like the tests to be as robust as possible.
因为我知道有时候我绝对需要WebDriverWait
,这是否意味着我需要在我的unittest
setUp
方法中摆脱implicit_wait
,而是每次使用时都要使用WebDriverWait
任何find_element_by_
方法?
Since I know there are times I'll definitely need WebDriverWait
, does this mean I need to get rid of implicit_wait
in my unittest
setUp
method and instead employ WebDriverWait
every single time I use any find_element_by_
method?
(我不想这样做;尽管我想我可以将每个find_element_by_
方法放在我自己的自定义函数中-每个方法都包装在自己的WebDriverWait
中-感觉我不应该拥有).
(I'd rather not have to do this; although I suppose I could put each of the find_element_by_
methods in my own custom functions -each wrapped in their own WebDriverWait
-it feels like I shouldn't have to).
所以我的主要问题是:
我可以将implicit_wait
保留在测试setUp
方法中,然后仅在find_elements_by_
和我知道需要的其他地方使用WebDriverWait
吗?
Can I instead keep my implicit_wait
in my test setUp
method, and then only use WebDriverWait
when it comes to find_elements_by_
and other places where I know I need it?
推荐答案
由于我知道有时候我绝对需要WebDriverWait,这是否意味着我需要在我的unittest setUp方法中摆脱implicit_wait,而每次使用任何find_element_by_方法时都使用WebDriverWait吗?
Since I know there are times I'll definitely need WebDriverWait, does this mean I need to get rid of implicit_wait in my unittest setUp method and instead employ WebDriverWait every single time I use any find_element_by_ method?
是的.正如您在链接到的问题中所看到的那样,如果同时使用两种类型的等待,您将遇到不良行为.这不只是理论上的.我亲身经历了这种行为,尝试对其进行调试,找到您链接的问题,然后从我的测试套件中删除了所有隐式等待.
Yes. As you've seen in the question you link to, if you use both types of waits, you're going to run into undesirable behavior. It's not just theoretical. I've experienced that behavior first hand, tried to debug it, found the question you linked to and then removed all implicit waits from my test suites.
我开发了一个库,以帮助设置显式等待(并做了很多其他事情).假设您已经有一个具有硒Web驱动程序的driver
对象:
I developed a library to help with setting up the explicit waits (and do a bunch of other things). Assuming you already have a driver
object that has the selenium web driver:
from selenium.webdriver.common.by import By
import selenic.util
util = selenic.util.Util(driver)
# This goes through util and uses the explicit wait set by util.
foo = util.find_element((By.CSS_SELECTOR, "..."))
# For special cases that take longer to give results.
with util.local_timeout(10):
# The timeout is set to 10 for calls in this with block.
bar = util.find_element(...)
# The timeout is restored to what it was just before the with.
有些时候根本不需要使用等待,因为逻辑上如果元素A存在,那么元素B也存在,因此您不必parent = foo.find_element_by_xpath("..")
.
There are some times when you don't need to use waits at all because logically if element A is present then B is also present, so you don't have to wait for it. For instance. if you want the parent of an element you've already obtained from Selenium, you can do parent = foo.find_element_by_xpath("..")
.
关于find_elements
的行为,它在确定有结果要返回时立即返回.这可能意味着如果find_elements
找到要返回的内容之后出现以后的元素,则只会得到一个元素.
As for the behavior of find_elements
, it returns as soon as it determines that there is a result to return. This could mean getting only one element if later elements do show up after find_elements
has found something to return.
这篇关于混合隐式和显式等待的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!