Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Wait until loader disappears python selenium

Tags:

<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 want to wait until it disappears. I have following code but it wait sometimes too long and at some point of code it suddenly freeze all 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 this one:

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 is there, the only thing that changes is parameter display changes from none to block. I think I can get attribute like string and check if there is word "block" but it is so wrong I thing... Help me please.

like image 611
Michael Avatar asked Sep 28 '14 16:09

Michael


People also ask

How do I make Selenium wait 10 seconds?

We can make Selenium wait for 10 seconds. This can be done by using the Thread. sleep method. Here, the wait time (10 seconds) is passed as a parameter to the method.

How do you use wait until an element is visible?

Selenium: Waiting Until the Element Is Visiblevar wait = new WebDriverWait(driver, TimeSpan. FromSeconds(20)); As you can see, we give the WebDriverWait object two parameters: the driver itself and a TimeSpan object that represents the timeout for trying to locate the element.

How to add delay in Selenium WebDriver Python?

exceptions import TimeoutException browser = webdriver. Firefox() browser. get("url") delay = 3 # seconds try: myElem = WebDriverWait(browser, delay).


2 Answers

Reiterated your answer (with some error handling) to make it easier for people to find the solution :)

Importing required classes:

from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.support.wait import WebDriverWait

Configuration 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 solution:

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 
like image 90
ChickenFeet Avatar answered Sep 18 '22 07:09

ChickenFeet


Use expected condition : invisibility_of_element_located

This works fine 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")))
like image 25
Suraj Shrestha Avatar answered Sep 19 '22 07:09

Suraj Shrestha