python - How to use Selenium find_element_by_something function with no wait time if the element is not there? -
sometimes want action on object may or may not in page, want execute action if object present on page, if object not present ok, don't want automation break. right now, have been handling situation in 2 different ways:
using find_elements_by_something method along if:
my_var = driver.find_elements_by_xpath(some_xpath) if len(my_var) > 0: # element present action using find_element_by_something method along try:
try: my_var = driver.find_element_by_id(some_id) # action except: # else both methods mentioned above work, have big problem, both make automation slow because both try find element(s) using driver timeout, can take while.
is there way try find element without using timeout? if element not there in instant in time ok, don't want selenium wait for while, want know if element there or not.
solution:
based on response @grumpasaurus
driver.implicitly_wait(0) try: my_var = driver.find_element_by_id(some_id) # action except: # else driver.implicitly_wait(old_wait_time)
i believe you're looking explicit/implicit wait situation.
http://docs.seleniumhq.org/docs/04_webdriver_advanced.jsp
selenium allows change implicit wait go through test change how long want wait element show up. technically can change 0 have rescue clause handy if don't want fail test.
Comments
Post a Comment