Search code examples
python-3.xseleniumwaitexpected-condition

Wait for text in an element to be a number in Selenium with Python


A dynamically generated website has such an element:

<b class="2020-is-terrible"></b>

After some time it fills the b element with a random number like this:

<b class="2020-is-terrible">42</b>

I want to wait for that number to be generated and for now I've come up a code that waits for a specific number:

WebDriverWait(driver, 10).until(expected_conditions.text_to_be_present_in_element((By.CLASS_NAME, "2020-is-terrible"), "42"))

But I want to continue if any value appears in the b element (not just 42). The value will always be a number so I just need to check if the text not empty.

List of expected_conditions classes so you don't need to google.


Solution

  • With xpath you can also use the and operator by combine class name and text!='', like this:

    //b[@class='2020-is-terrible' and text()!='']

    Try this:

    WebDriverWait(driver, 10).until(expected_conditions.presence_of_element_located((By.XPATH, "//b[@class='2020-is-terrible' and text()!='']")))