Python Forum

Full Version: WebDriverWait improved function
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
hi guys,
I'm trying to create some kind of improved WebDriverWait function.
the problem I face automating a webpage is that it requires that ugly description for each WebDriverWait command, if you don't want just stuck at that point.
try:
   WebDriverWait(drive, waitT).until(EC.visibility_of_element_located((By.ID, element_name)))
except TimeoutException:
   quit()
I want to create is as one-line function call. I have created this function, but it's ugly and slow downs overall performance a little bit. Maybe you have some other ideas how to do that? I'm a noob in Python :)
def WebWait(drive, waitT, wait_type, element_type, element_name):
    try:
        if wait_type == "visible":
            if element_type == "ID":
                WebDriverWait(drive, waitT).until(EC.visibility_of_element_located((By.ID, element_name)))
            elif element_type == "CLASS_NAME":
                WebDriverWait(drive, waitT).until(EC.visibility_of_element_located((By.CLASS_NAME, element_name)))
            elif element_type == "TAG_NAME":
                WebDriverWait(drive, waitT).until(EC.visibility_of_element_located((By.TAG_NAME, element_name)))
            elif element_type == "XPATH":
                WebDriverWait(drive, waitT).until(EC.visibility_of_element_located((By.XPATH, element_name)))
            elif element_type == "NAME":
                WebDriverWait(drive, waitT).until(EC.visibility_of_element_located((By.NAME, element_name)))
        elif wait_type == "click":
            if element_type == "ID":
                WebDriverWait(drive, waitT).until(EC.element_to_be_clickable((By.ID, element_name)))
            elif element_type == "CLASS_NAME":
                WebDriverWait(drive, waitT).until(EC.element_to_be_clickable((By.CLASS_NAME, element_name)))
            elif element_type == "TAG_NAME":
                WebDriverWait(drive, waitT).until(EC.element_to_be_clickable((By.TAG_NAME, element_name)))
            elif element_type == "XPATH":
                WebDriverWait(drive, waitT).until(EC.element_to_be_clickable((By.XPATH, element_name)))
            elif element_type == "NAME":
                WebDriverWait(drive, waitT).until(EC.element_to_be_clickable((By.NAME, element_name)))
    except TimeoutException:
        ctypes.windll.user32.MessageBoxW(0, "", "Task Failed", 0)
        quit()

# the call:
WebWait(driver, waiting_time, "visible", "ID", "btnCaseSearch")