Make Selenium wait until text inside web element changes

Question:

I’m using Selenium to upload an excel file to a website automatically. To make sure upload is complete before proceeding i use time.sleep(60) but i want to make the code a bit smarter.
after the upload field, there’s a label element.

<label id="upload_status">only .xlsx files will be accepted.</label>

this label changes after upload is finished to

<label id="upload_status">100 entries were detected.</label>

can i use the text inside the label to tell if the upload has finished?

Asked By: Amir

||

Answers:

Yes, you can use text_to_be_present_in_element expected_conditions.
This should work:

WebDriverWait(driver, 100).until(EC.text_to_be_present_in_element((By.ID, "upload_status"), "entries were detected"))

These imports should be used here:

from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
Answered By: Prophet

First find label element by ID, then check for keyword to (dis)appear:

from selenium import webdriver
from selenium.webdriver.common.by import By
...
el = driver.find_element(By.ID, 'upload_status')
while 'entries were detected' not in el.get_attribute('innerHTML'):
    time.sleep(1)
...
Answered By: CodingTea