Wait until the loader disappears pelon selenium
<div id="loader-mid" style="position: absolute; top: 118.5px; left: 554px; display: none;"> <div class="a">Loading</div> <div class="b">please wait...</div> </div> And I want to wait until he disappears. I have the following code, but it lingers too long, and at some point in the code, it suddenly freezes the whole process, and I donβt know why.
from selenium.webdriver.support import expected_conditions as EC from selenium.webdriver.common.by import By from selenium.webdriver.support.wait import WebDriverWait self.wait = WebDriverWait(driver, 10) self.wait.until(EC.invisibility_of_element_located((By.XPATH, "//*[@id='loader_mid'][contains(@style, 'display: block')]"))) and also i tried the following:
self.wait.until_not(EC.presence_of_element_located((By.XPATH, "//*[@id='loader_mid'][contains(@style, 'display: block')]"))) I donβt know exactly how to check, but maybe my element is always present on the page, and selenium thought that it was there, the only thing that was changing was changing the display settings from none to block. I think I can get the attribute as a string and check if there is a word "block", but this is so wrong. I ... Help me please.
Confirmed your answer (with some error handling) to make it easier for people to find a solution :)
Final variables
SHORT_TIMEOUT = 5 # give enough time for the loading element to appear LONG_TIMEOUT = 30 # give enough time for loading to finish LOADING_ELEMENT_XPATH = '//*[@id="xPath"]/xPath/To/The/Loading/Element' Code somewhere else in the py file
try: # wait for loading element to appear # - required to prevent prematurely checking if element # has disappeared, before it has had a chance to appear WebDriverWait(driver, SHORT_TIMEOUT ).until(EC.presence_of_element_located((By.XPATH, LOADING_ELEMENT_XPATH))) # then wait for the element to disappear WebDriverWait(driver, LONG_TIMEOUT ).until_not(EC.presence_of_element_located((By.XPATH, LOADING_ELEMENT_XPATH))) except TimeoutException: # if timeout exception was raised - it may be safe to # assume loading has finished, however this may not # always be the case, use with caution, otherwise handle # appropriately. pass The following code creates an endless loop until the element disappears:
from selenium.webdriver.common.by import By from selenium.webdriver.support.ui import WebDriverWait from selenium.webdriver.support import expected_conditions as EC from selenium.common.exceptions import TimeoutException while True: try: WebDriverWait(driver, 1).until(EC.presence_of_element_located((By.XPATH, 'your_xpath'))) except TimeoutException: break Use the expected condition: invisibility_of_element_located
This works great for me.
from selenium.webdriver.common.by import By from selenium.webdriver.support import expected_conditions as EC from selenium.webdriver.support.wait import WebDriverWait WebDriverWait(driver, timeout).until(EC.invisibility_of_element_located((By.ID, "loader-mid")))