Measure the time page element is visible on the page with selenium

Question:

When I interact with the page, loader appears for couple of seconds, and then it disappears completely from page html.

<div class="loader">
<img src="/static/media/loading-anim-windmill.9f645983.gif" alt="loading"><p>Loading...</p>
</div>

for waiting this element to appear i simply write

element = WebDriverWait(elem, 10).until(EC.element_to_be_clickable((by, val)))

but how do I know when did it disappear? and how do i measure this time range?

Asked By: Saba

||

Answers:

Similarly to element_to_be_clickable expected condition there are invisibility_of_element_located and invisibility_of_element expected conditions.
So, to wait for element to disappear you can do something like this:

wait = WebDriverWait(driver, 10)
wait.until(EC.invisibility_of_element((By.CSS_SELECTOR, "the_css_selector")))

To measure the time the elements disappears you can do as following:

startTime = time.time()
wait = WebDriverWait(driver, 10)
wait.until(EC.invisibility_of_element((By.CSS_SELECTOR, "the_css_selector")))
finishTime = time.time()
print(finishTime-startTime)
Answered By: Prophet