25. Handling Dynamic Elements, Spinners & Stale References

Handling Spinners & Stale Elements in Python

from selenium.common.exceptions import StaleElementReferenceException

# Wait for spinner disappearance:
WebDriverWait(driver, 10).until(
EC.invisibility_of_element_located((By.CSS_SELECTOR, ".spinner-icon"))
)

# Retry loop for stale element references:
def click_with_retry(driver, locator, max_attempts=3):
for _ in range(max_attempts):
try:
driver.find_element(*locator).click()
return
except StaleElementReferenceException:
pass
raise RuntimeError(f"Element remained stale: {locator}")

Task

  • Implement wait_for_spinner_gone(driver, spinner_locator):
  • 1. Return WebDriverWait(driver, 10).until(EC.invisibility_of_element_located(spinner_locator)).
πŸ’‘Common doubts
Did you like the problem?
Lesson 25 of 43
Python
Loading code editor…
Next