4

When using Selenium/Python, I need to wait/pause until: style="display:none;" is displayed for a <div id="notification"..</div>


After clicking a button, the following is displayed (Loading...)

<div id="notification" class="notification_info" style="opacity: 1; display: inline-block;">Loading...</div>

Then after a dataset is loaded in the web page, the Loading... goes away (by changing display:none), and the following is present:

<div id="notification" class="notification_info" style="display: none;">Loading...</div>


How would this be done (to check or wait on the value of style="display: none;")?

Because there are many <divs> in the page with style=display, I need to wait on both the id of the div, and the style display.

undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
user10664542
  • 1,106
  • 1
  • 23
  • 43

1 Answers1

4

Once you click the desired button the element with text as Loading... becomes visible. Hence you see the element within the HTML DOM as:

<div id="notification" class="notification_info" style="opacity: 1; display: inline-block;">Loading...</div>

Once the loading completes the the element with text as Loading... is made invisible by changing the display property of the style attribute as:

style="display: none;"

Hence the WebElement is represented in the DOM Tree as:

<div id="notification" class="notification_info" style="display: none;">Loading...</div>

Solution

Using Selenium to wait for the the element with text as Loading... to turn as style="display: none;" you need to induce WebDriverWait for the invisibility_of_element() and you can use either of the following Locator Strategies:

  • Using CSS_SELECTOR:

    WebDriverWait(driver, 20).until(EC.invisibility_of_element((By.CSS_SELECTOR, "div.notification_info#notification")))
    
  • Using XPATH:

    WebDriverWait(driver, 20).until(EC.invisibility_of_element_located((By.XPATH, "//div[@class='notification_info' and @id='notification']")))
    
  • Note : You have to add the following imports :

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

Reference

You can find a couple of relevant detailed discussions in:

undetected Selenium
  • 183,867
  • 41
  • 278
  • 352