Last active
August 29, 2015 14:11
-
-
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)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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