Skip to content

Instantly share code, notes, and snippets.

@eyalzek
Last active August 29, 2015 14:11
Show Gist options
  • Save eyalzek/4502beb341b8124d1331 to your computer and use it in GitHub Desktop.
Save eyalzek/4502beb341b8124d1331 to your computer and use it in GitHub Desktop.
Wait for visibility function for selenium - focus on element if not in viewport (py)
def wait_for_visibility(self, selector, timeout_seconds=5):
retries = timeout_seconds
pause_interval = 2
while retries:
try:
element = self.driver.find_element_by_css_selector(selector)
if element.is_displayed():
return element
elif "visible" in element.value_of_css_property("visibility"):
print("trying to focus on element")
self.driver.execute_script("$(\"" + selector + "\").focus()")
except (exceptions.NoSuchElementException,
exceptions.StaleElementReferenceException):
if retries <= 0:
raise
else:
pass
retries = retries - 1
time.sleep(pause_interval)
raise exceptions.ElementNotVisibleException(
"Element {} not visible despite waiting for {} seconds".format(
selector, timeout_seconds * pause_interval)
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment