How to check an item contains ANY text?

I often need to wait for an AJAX call to add text to an element on my pages after the download is complete. I understand how to use WebDriverWait to wait for a certain text to be present in an element, but I do not see how to wait until any text appears. I am trying to avoid a while loop that continues to check the text of a non == '' element.

Here 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 any way to check any text or non-empty string?

+6
source share
1 answer

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

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

FYI, I am using presence_of_element_located() here:

Waiting to verify that the item is present in the DOM p. This does not necessarily mean that the item is visible.

Full 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') 
+9
source

Source: https://habr.com/ru/post/972105/


All Articles