pythonseleniumselenium-webdriverwebdriverwaitselenium-edgedriver

Selenium Explicit Waits for invisibility_of_element() not waiting at all


I am trying to make Selenium wait until a loader div is invisible.

These are my imports:

from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.support.ui import WebDriverWait, Select
from selenium.webdriver.support.ui import expected_condition as EC

I first click on a button to open a pop up window with table data in it.

action.click()
action.perform()

After clicking I have to wait until I can press a button to export this data in a file. However, the time I have to wait varies wildly. Sometimes 10 seconds sometimes a few minutes. While this section is loading a loader shows up which prevents me from clicking anywhere on the screen.

I am trying to make Selenium wait until this loader is gone. However, for some reason, the script does not wait at all. Not even the maximum time, which is passed into the explicit wait function.

time.sleep(10)
print("Waiting for button")

wait = WebDriverWait(driver, 30) # I am just testing with 30, it will be a larger value
wait.until(EC.invisibility_of_element((By.XPATH, "//div[@class='loader']")))

print("Finished Waiting for button")

driver.find_element_by_xpath("//button[@class='export']").click()

First I am making Selenium wait 10 seconds so that the loader element can actually show up, which already shows up in 1-2 seconds. After that I use print statement to check how long the script actually wait. The script does not wait at all. It immediately continues which then causes an error because the button is not clickable yet.


Solution

  • You are passing a By.locator so instead of invisibility_of_element() you must be using invisibility_of_element_located().

    First to wait for the visibility and then for the invisibility of the element you need to: