Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to wait for presence of an element with non empty content?

How can I wait for a presence of an element which the content (text) is not empty? I tried select by xpath using //*[@id="test" and text() != ""], but the return of WebDriverWait#until does not retrieve the element.

My code:

selector = '//*[@id="test" and text() != ""]'
element = WebDriverWait(driver, timeout).until(
  expected_conditions.presence_of_element_located((By.XPATH, selector))
)

I would like to get the text content of the element. I tried:

print element.text # prints 0

If i print only element, the output is <selenium.webdriver.remote.webelement.WebElement(session="xxx", element="xxx")>. What is wrong?

The div I'm tryin to get has this structure:

<div>
   Test:
   <div id="test"><b>this text here</b></div>
</div>
like image 792
Renan Gomes Avatar asked Oct 15 '25 14:10

Renan Gomes


1 Answers

You can wait for an element by xpath with non empty text like this:

xpath = "//*[@id='test']"

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

source

like image 117
Sergey Avatar answered Oct 18 '25 10:10

Sergey



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!