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.
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.
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("..")
.
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.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…