Home > Mobile >  How can I use EC.presence_of_element_located in selenium searching by the xpath of child element (re
How can I use EC.presence_of_element_located in selenium searching by the xpath of child element (re

Time:06-23

I would like to make use of EC.presence_of_element_located to check that a button is present, So I tried with this:

def _check_button_is_present(driver: WebDriver, row: WebElement) -> bool:
    """
    Check button is present
    """
    ELEMENT_ID = 'myButtonPartialId'
    try:
        element: WebElement = WebDriverWait(driver, timeout=30).until(
            EC.visibility_of_element_located(
                (   
                    By.XPATH,
                    row.find_element_by_xpath(f'.//button[contains(@id, "{ELEMENT_ID}")]')
                )
            )
        )
        logger.debug(f'Button found')

    except TimeoutException:
        logger.error(f'Element "{ELEMENT_ID}" could not be found')
        return False

    return True

But the method expects a locator, so switching to just:

            EC.visibility_of_element_located(
               row.find_element_by_xpath(f'.//button[contains(@id, "{ELEMENT_ID}")]')
            )

Does not make the trick as for example wait until element is clickable.

How can I pass a relative xpath to an element to visibility_of_element_located?

CodePudding user response:

If I understand correctly you need

WebElement = WebDriverWait(row, timeout=30).until(
            EC.visibility_of_element_located(
                (   
                    By.XPATH,
                    f'.//button[contains(@id, "{ELEMENT_ID}")]'
                )
            )

Note that you need to pass row instead of driver as argument to WebDriverWait

  • Related