Last active
October 29, 2018 19:44
-
-
Save uluQulu/712cff0d89f18e0fe5f46dc137d4f8b3 to your computer and use it in GitHub Desktop.
Generic explicit wait function for InstaPy to be used with selenium webdriver
This file contains hidden or 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 explicit_wait(browser, track, ec_params, logger, timeout=35, notify=True): | |
""" | |
Explicitly wait until expected condition validates | |
:param browser: webdriver instance | |
:param track: short name of the expected condition | |
:param ec_params: expected condition specific parameters - [param1, param2] | |
:param logger: the logger instance | |
""" | |
# list of available tracks: | |
# <https://seleniumhq.github.io/selenium/docs/api/py/webdriver_support/ | |
# selenium.webdriver.support.expected_conditions.html> | |
if not isinstance(ec_params, list): | |
ec_params = [ec_params] | |
# find condition according to the tracks | |
if track == "VOEL": | |
elem_address, find_method = ec_params | |
ec_name = "visibility of element located" | |
find_by = (By.XPATH if find_method == "XPath" else | |
By.CSS_SELECTOR if find_method == "CSS" else | |
By.CLASS_NAME) | |
locator = (find_by, elem_address) | |
condition = ec.visibility_of_element_located(locator) | |
elif track == "TC": | |
expect_in_title = ec_params[0] | |
ec_name = "title contains '{}' string".format(expect_in_title) | |
condition = ec.title_contains(expect_in_title) | |
elif track == "PFL": | |
ec_name = "page fully loaded" | |
condition = (lambda browser: browser.execute_script("return document.readyState") | |
in ["complete" or "loaded"]) | |
# generic wait block | |
try: | |
wait = WebDriverWait(browser, timeout) | |
result = wait.until(condition) | |
except TimeoutException: | |
if notify == True: | |
logger.info("Timed out with failure while explicitly waiting until {}!\n" | |
.format(ec_name)) | |
return False | |
return result | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment