Pastebin

Paste #25032: No description

< previous paste - next paste>

Pasted by Anonymous Coward

Download View as text


def find_text_in_elements(driver, locator, target_text, timeout=30):
    """
    Searches through the text content of elements matching 'locator' until it finds text containing 'target_text'
    or until timeout is reached

    :param driver: webdriver
    :param locator: locator tuple
    :param target_text: text string to search for in text content
    :param timeout: timeout in seconds
    :return: (execution time, first element containing matching text)
    """
    logger.debug("actions.find_text_in_elements")

    # we sleep 10 ms per iteration, so we will have to do (timeout * 100) iterations at max
    _start_time = time.perf_counter()
    for i in range(int(timeout * 100)):
        elems = driver.find_elements(*locator)
        logger.debug(
            f"find_text_in_elements[iteration {i}] Found {len(elems)} elements (locator={locator}, target_text={target_text}")
        for elem in elems:
            if target_text in elem.text.strip():
                logger.debug(f"  Found target text '{target_text}' in elem.text: '{elem.text}'")
                return time.perf_counter() - _start_time, elem
            else:
                # logger.debug(f"  Did NOT find target text '{target_text}' in elem.text: '{elem.text}'. Sleeping...")
                pass
            time.sleep(0.01)
    logger.error("Timeout occurred before finish condition was reached")
    return time.perf_counter() - _start_time, None



target_text = "Activity"
activity_selector = (By.CSS_SELECTOR, ".tabLabel")
_, activity_elem = find_text_in_elements(driver, activity_selector, target_text)

New Paste


Do not write anything in this field if you're a human.

Go to most recent paste.