Below is my current favorite method to wait for an element to appear or become useful on a dynamic web page. In this case, my example is avoiding the exception thrown when webdriver fails to find an element by using the ‘find_elements’(plural) method rather than a ‘find_element’(singular) The ‘find_elements’ methods always return a list, even if empty, rather than throw an exception. Both are useful, but in this case I like the more readable code without the try/except requirements. I also like to have a way to record wait times for specific events. Here I’m just printing that info out for example, but I often record this in a db for charting or analysis later.
SOME_CSS_SELECTOR = '#interesting_element_selector' MAX_WAIT = 5 # my style, this is often defined as a class constant . . . waited = 0 while waited < self.MAX_WAIT: # wait for something. I most often put 'sleep(0) just before the 'waited+=1' # but put it here in cases where I know there is a slow element load sleep(1) elements = self.driver.find_elements_by_css_selector(self.SOME_CSS_SELECTOR) if len(elements) > 1: break else: loading_indicator_element = loading_indicator_elements waited += 1 print '[INFO] Waited %i seconds for %s' % (waited, str(self.SOME_CSS_SELECTOR))