9

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?

1 Answer 1

15

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') 
Sign up to request clarification or add additional context in comments.

5 Comments

Thanks for the reply! I can't seem to get the syntax right for your example. By.xpath has to be By.XPATH I believe, but I still get a TypeError. Also, what is the significance of 'foo' if I don't care what the text contains?
@Jesse yeah, xpath was a typo - see the updated answer with a complete code. Check if it works for you. Thanks.
Ok, just saw your updated answer. It seems like it should work, but it's still timing out for me. Example showing the element has text, and then the WebDriverWait timing out - http://bpaste.net/show/7kl2xNhrrPNTr443YH1M/
@Jesse I see, myElem is not a tag name - it is an id attribute value. Correct your xpath: //*[@id="myElem" and text() != ""].
D'oh, I should've caught that! It works! Interesting how it returns a WebElement object where my original example with text_to_be_present_in_element() returned True.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.