Sometimes you want to do an action over an object that may or may not be in a page, obviously you would only want to execute the action if the object is present on the page, but if the object is not present that is also ok, but you don't want the automation to break. Right now, I have been handling this situation in two different ways:
Using a find_elements_by_something method along with an if:
my_var = driver.find_elements_by_xpath(some_xpath)
if len(my_var) > 0:
# the element is present so do the action
Using a find_element_by_something method along with a try:
try:
my_var = driver.find_element_by_id(some_id)
# do the action
except:
# do something else
Both methods mentioned above work, but they have a big problem, they both make the automation slow because both try to find the element(s) using the driver timeout, so it can take a while.
Is there a way to try to find an element without using any timeout? If the element is not there in that instant in time that is ok, I don't want selenium to wait for it for a while, I just want to know if the element is there or not.
Solution:
Based on the response from @grumpasaurus
driver.implicitly_wait(0)
try:
my_var = driver.find_element_by_id(some_id)
# do the action
except:
# do something else
driver.implicitly_wait(old_wait_time)