Skip to content
CodeKruPro

Set an Implicit Wait

38/80

Set an Implicit Wait

implicitly_wait(5) tells later find calls to poll for up to 5 seconds. On this page #firstName is already present, so finding it does not prove that any waiting happened. driver.timeouts.implicit_wait reports the wait as a float, 5.0, so wrap it in int() to print a whole number.

Task:

  1. Call implicitly_wait(5)
  2. Open Text Fields and find the first #firstName
  3. Print whether that field is displayed
  4. Print the implicit wait driver.timeouts.implicit_wait reports, as a whole number

Do not mix an implicit wait with an explicit wait in one script. The two clocks make a timeout hard to interpret.

Print the lines below exactly.

Expected output:

Element found: True
Implicit wait set: 5 seconds

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

  1. Call implicitly_wait with 5 inside the try block, before the page loads.
  2. Open the Text Fields page and find the first firstName field.
  3. Print whether that field is displayed, converting the boolean with str().
  4. Read driver.timeouts.implicit_wait, convert the float with int() and then str(), 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() raises TypeError: '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.

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.

main.py
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()