About this lesson
Selenium Python implicitly_wait: set an implicit wait and read it back from driver.timeouts
Selenium Python AssignmentsIntermediateWaits
How it works
An implicit wait is one setting on the driver. After driver.implicitly_wait(5), every later find_element or find_elements call retries for up to five seconds before giving up. If the element shows up sooner, the call returns at once. You set it once and it applies for the whole session.
The unit is seconds, not milliseconds. A negative number raises InvalidArgumentException ("value must be a non-negative integer"), and 5000 means 5000 seconds.
You can read the setting from driver.timeouts.implicit_wait. It is an attribute, not a method, and it returns a float: a fresh driver reports 0.0, and after the call above it reports 5.0. Convert with int() for a whole number, then str() for joining.
Missing elements pay the full price. A find for an id that is not on the page took five seconds to raise NoSuchElementException, and find_elements took the same time to return an empty list. This task only proves the setting, because the field is already there.
Do not mix an implicit wait with WebDriverWait in one script.
About the assignment
Set a five second implicit wait on the driver, load the Text Fields page, find the first name field, and report that it is visible and what the driver has stored.
What you will do
- Call
implicitly_waitwith 5 inside thetryblock, before the page loads. - Open the Text Fields page and find the first
firstNamefield. - Print whether that field is displayed, converting the boolean with
str(). - Read
driver.timeouts.implicit_wait, convert the float withint()and thenstr(), and print it with the unit shown in the task.
Common mistakes
Printing the float as it is
Without
int()the value prints with a decimal point, which is not the whole number the task asks for. Convert it first.Calling implicit_wait like a method
driver.timeouts.implicit_wait()raisesTypeError: 'float' object is not callable. It is a plain attribute holding a number.Expecting it to fix a wrong locator
The wait only helps with elements that appear late. A locator that is simply wrong waits the full time and then raises
NoSuchElementException.
In a real test suite
Many teams avoid implicit waits and use explicit waits only, because the global timer slows every failing find and hides real problems. When you do see one, it sits in a conftest.py fixture right after the driver is created.
The next lesson shows the explicit alternative, which waits for one named condition on one element.
Related lessons
- Wait Until an Element Is VisibleWait for one specific element with
WebDriverWaitinstead of a global timer. - Diagnose a Wait That Times OutDiagnose a wait that times out, where the time spent matters.
- Check Whether an Element Is DisplayedUse
is_displayed(), the check this task prints after finding the field.
To try one of these calls on any page without a task, open the Online Selenium Python Compiler.
Starter code
The editor above opens with this code. Fill in the TODO lines, then run and submit it.
from selenium import webdriver
from selenium.webdriver.common.by import By
driver = webdriver.Chrome()
try:
# Set a 5 second implicit wait, open https://testkru.com/Elements/TextFields, and find #firstName
pass
finally:
driver.quit()