Skip to content

Instantly share code, notes, and snippets.

@kennethkoontz
Created August 15, 2012 21:34
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kennethkoontz/3363896 to your computer and use it in GitHub Desktop.
Save kennethkoontz/3363896 to your computer and use it in GitHub Desktop.
looping construct
locator = '.ui-dialog'
timeout = 10
try:
for x in range(timeout):
self.wd.find_element_by_css_selector(locator)
time.sleep(1)
raise TimeoutException('%r still present after %s seconds' % (locator, timeout))
except NoSuchElementException:
pass
@kennethkoontz
Copy link
Author

Couple things to keep in mind. 'find_element_by_css_selector(locator) will raise no Such Element exception when element is not found.

@SethAThomas
Copy link

What are you trying to accomplish? It looks like you are testing for something to be removed.

timeout is a misleading name. Generally, that means how long you are going to pause something. timeout => maxTries

timeout and sleep time should be customizable. 10 and 1 might be the defaults you want.

Should be a function.

If you unroll the loop, you'll see that there is an unnecessary sleep at the end. Pretend we are going to try 3 times:

find(locator)
sleep(1)
find(locator)
sleep(1)
find(locator)
sleep(1) // unnecessary, since we aren't testing anymore

This also means that something only has 9 seconds to be removed, not the 10 that you think.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment