Skip to content
CodeKruPro

A custom wait condition with a lambda (Python)

QuizMediumPython onlyAsked in: Infosys, TCS, Wipro
wait = WebDriverWait(driver, 30, poll_frequency=0.5,
                     ignored_exceptions=[StaleElementReferenceException])
ok = wait.until(lambda d: d.find_element(By.ID, "status").text == "Completed")

What does this do?

  • Every 0.5 s it reads the status; it keeps waiting while the element is missing, stale or not yet Completed, then returns True; after 30 s it raises TimeoutException
  • It raises NoSuchElementException at once if the element is missing
  • It returns False after 30 seconds if the text never changes
  • It waits a fixed 30 seconds before checking

This question is part of Pro+

Pro+ opens every question in Interview Prep and the mock interview. Every article and the first 2 questions of each topic stay free. Hands-on Selenium and API tasks follow their course lesson's own lock.

See Pro+

Already have Pro+? Sign in