Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to verify an element contains ANY text?

I often need to wait for an AJAX call to add text to an element on my pages after they finish loading. I understand how to use WebDriverWait to wait for specific text to be present in the element, but I don't see how to wait until there is ANY text present. I'm trying to avoid a while loop that keeps checking the element's text isn't == ''.

Here's what I use to find specific text:

try:
    WebDriverWait(self.driver, 10).until(EC.text_to_be_present_in_element((By.ID, 'myElem'), 'foo'))
except TimeoutException:
    raise Exception('Unable to find text in this element after waiting 10 seconds')

Is there a way to check for any text or a non-empty string?

like image 312
jjj Avatar asked Jul 11 '14 19:07

jjj


1 Answers

You can use By.XPATH and check if the text() is non-empty inside the xpath expression:

EC.presence_of_element_located((By.XPATH, '//*[@id="myElem" and text() != ""]'))

FYI, I'm using presence_of_element_located() here:

An expectation for checking that an element is present on the DOM of a page. This does not necessarily mean that the element is visible.

Complete code:

try:
    WebDriverWait(self.driver, 10).until(EC.presence_of_element_located((By.XPATH, '//*[@id="myElem" and text() != ""]')))
except TimeoutException:
    raise Exception('Unable to find text in this element after waiting 10 seconds')
like image 68
alecxe Avatar answered Oct 13 '22 20:10

alecxe