Wednesday, November 21, 2018

Python - How to wait for presence of an element with non empty text?

Hello!

Sometimes it's useful to wait for an element with non empty text. The question is how to do it with Selenium in Python? Pretty easy! You can find an answer below.

If xpath is absolute, then you can wait for your element with non empty text like this:
# xpath - xpath of your element

WebDriverWait(driver, 30).until(lambda driver: driver.find_element_by_xpath(xpath).text.strip() != '')


If xpath is relative to some other element, for example parent, then you can do this:
# parent is an element which contains child element, which we want to wait for.
# xpath is relative to parent

WebDriverWait(driver, 30).until(lambda parent: parent.find_element_by_xpath(xpath).text.strip() != '')

That's it

No comments:

Post a Comment