Search code examples
pythonseleniumwebdriverappium

Viable locator strategies for partial link text in webdriver


I'm testing a messaging app, and I'm planning to have the code wait until it receives the message, "Hello, my name is Jeff."

2 pieces of failed code I tried:

msg_receive_1 = WebDriverWait(driver1, 15).until(
        EC.visibility_of_element_located((By.PARTIAL_LINK_TEXT, "Hello"))
    )
msg_receive_1 = WebDriverWait(driver1, 15).until(
        EC.visibility_of_element_located((By.LINK_TEXT, "Hello, my name is Jeff."))
    )

Is visibility of element located a sub-par choice for partial link text? If so, what would be a viable alternative?

If the issue is something else, what could it be?


Solution

  • Your question

    Is visibility of element located a sub-par choice for partial link text?

    No, absolutely no.

    See what is underneath of visibility_of_element_located Method :-

    """ An expectation for checking that an element is present on the DOM of a
    page and visible. Visibility means that the element is not only displayed
    but also has a height and width that is greater than 0.
    locator - used to find the element
    returns the WebElement once it is located and visible
    """
    
    class visibility_of_element_located(object):
        def __init__(self, locator):
            self.locator = locator
    
        def __call__(self, driver):
            try:
                return _element_if_visible(_find_element(driver, self.locator))
            except StaleElementReferenceException:
                return False
    

    so basically it calls,

    _element_if_visible
    

    and this is what we have

    def _element_if_visible(element, visibility=True):
        return element if element.is_displayed() == visibility else False
    

    So, direct answer to your question is No. But it depends, how you have defined EC, in your case, Are you sure if that is the unique link_text or partial_link_text, did you try changing that to css or xpath and verified like below ?

    //*[contains(text(),'Hello')]
    

    in code :

    msg_receive_1 = WebDriverWait(driver1, 15).until(
            EC.visibility_of_element_located((By.XPATH, "//*[contains(text(),'Hello')]"))
        )
    

    as an xpath ?