Bird
Raised Fist0
Selenium Pythontesting~10 mins

Time.sleep vs proper waits in Selenium Python - Interactive Practice

Choose your learning style10 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to pause the test for 5 seconds using time.sleep.

Selenium Python
import time

time.[1](5)
Drag options to blanks, or click blank then click option'
Adelay
Bsleep
Cpause
Dwait
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'wait' or 'pause' which are not functions in the time module.
Forgetting to import the time module.
2fill in blank
medium

Complete the code to wait until an element with ID 'submit' is clickable using Selenium's WebDriverWait.

Selenium Python
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC

wait = WebDriverWait(driver, 10)
wait.until(EC.[1]((By.ID, 'submit')))
Drag options to blanks, or click blank then click option'
Apresence_of_element_located
Balert_is_present
Cvisibility_of_element_located
Delement_to_be_clickable
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'presence_of_element_located' which only checks if element is in DOM, not clickable.
Using 'alert_is_present' which is unrelated to element clicking.
3fill in blank
hard

Fix the error in the code to properly wait for an element with class name 'loading' to disappear.

Selenium Python
wait = WebDriverWait(driver, 15)
wait.until(EC.[1]((By.CLASS_NAME, 'loading')))
Drag options to blanks, or click blank then click option'
Ainvisibility_of_element_located
Bpresence_of_element_located
Cvisibility_of_element_located
Delement_to_be_clickable
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'presence_of_element_located' which waits for presence, not disappearance.
Using 'visibility_of_element_located' which waits for visibility, opposite of what is needed.
4fill in blank
hard

Fill both blanks to create a dictionary comprehension that maps element IDs to their text only if the text length is greater than 3.

Selenium Python
elements_text = {el.get_attribute('id'): el.text for el in driver.find_elements(By.TAG_NAME, 'div') if len(el.[1]) [2] 3}
Drag options to blanks, or click blank then click option'
Atext
B>
C<
Dget_attribute('text')
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'get_attribute('text')' which is not a valid attribute.
Using '<' instead of '>' which filters shorter texts.
5fill in blank
hard

Fill all three blanks to create a dictionary comprehension that maps uppercase element IDs to their text only if the text contains the letter 'a'.

Selenium Python
elements_dict = {el.get_attribute([1]).[2](): el.text for el in driver.find_elements(By.CLASS_NAME, 'item') if 'a' [3] el.text}
Drag options to blanks, or click blank then click option'
A'id'
Bupper
Cin
Dcontains
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'contains' which is not a Python operator.
Forgetting to quote 'id' as a string.

Practice

(1/5)
1. What is the main difference between time.sleep() and proper waits in Selenium?
easy
A. Proper waits always wait longer than time.sleep().
B. time.sleep() pauses for a fixed time; proper waits wait only as long as needed.
C. time.sleep() is faster than proper waits in all cases.
D. time.sleep() waits until an element is clickable; proper waits pause for a fixed time.

Solution

  1. Step 1: Understand time.sleep() behavior

    time.sleep() pauses the test for a fixed number of seconds regardless of page state.
  2. Step 2: Understand proper waits behavior

    Proper waits wait only until a condition is met or a timeout occurs, making tests faster and more reliable.
  3. Final Answer:

    time.sleep() pauses for a fixed time; proper waits wait only as long as needed. -> Option B
  4. Quick Check:

    Fixed pause vs conditional wait = D [OK]
Hint: Remember: sleep waits fixed time; proper waits wait smartly [OK]
Common Mistakes:
  • Thinking time.sleep() waits for conditions
  • Assuming proper waits always wait full timeout
  • Believing time.sleep() is faster
2. Which of the following is the correct syntax to wait until an element is visible using Selenium's WebDriverWait in Python?
easy
A. driver.wait(10).until(EC.visibility_of_element_located('myId'))
B. time.sleep(10).until(EC.visibility_of_element_located((By.ID, 'myId')))
C. WebDriverWait(driver, 10).until(EC.visibility_of_element_located((By.ID, 'myId')))
D. WebDriverWait(driver, 10).wait_for_element('myId')

Solution

  1. Step 1: Recall correct WebDriverWait syntax

    The correct syntax uses WebDriverWait(driver, timeout).until(condition) with a locator tuple.
  2. Step 2: Check locator format and method

    WebDriverWait(driver, 10).until(EC.visibility_of_element_located((By.ID, 'myId'))) uses EC.visibility_of_element_located with (By.ID, 'myId') tuple, which is correct.
  3. Final Answer:

    WebDriverWait(driver, 10).until(EC.visibility_of_element_located((By.ID, 'myId'))) -> Option C
  4. Quick Check:

    Correct WebDriverWait syntax = C [OK]
Hint: Use WebDriverWait with until and locator tuple [OK]
Common Mistakes:
  • Using time.sleep instead of WebDriverWait
  • Passing locator as string instead of tuple
  • Calling non-existent wait_for_element method
3. What will happen when the following code runs if the element with ID 'submit' appears after 3 seconds?
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC

WebDriverWait(driver, 5).until(EC.element_to_be_clickable((By.ID, 'submit')))
print('Element is clickable')
medium
A. Prints 'Element is clickable' after about 3 seconds
B. Raises TimeoutException immediately
C. Prints 'Element is clickable' after 5 seconds fixed
D. Code syntax error, will not run

Solution

  1. Step 1: Understand WebDriverWait behavior

    It waits up to 5 seconds for the element to be clickable, checking repeatedly.
  2. Step 2: Element appears after 3 seconds

    Since element appears before timeout, wait ends early and prints message.
  3. Final Answer:

    Prints 'Element is clickable' after about 3 seconds -> Option A
  4. Quick Check:

    Wait ends early if condition met = A [OK]
Hint: Wait ends as soon as element is ready, not full timeout [OK]
Common Mistakes:
  • Thinking it waits full 5 seconds always
  • Expecting immediate TimeoutException
  • Confusing syntax causing errors
4. Identify the problem in this code snippet that uses time.sleep() instead of proper waits:
driver.find_element(By.ID, 'start').click()
time.sleep(10)
driver.find_element(By.ID, 'finish').click()
medium
A. No problem; this is best practice
B. The click() method is used incorrectly
C. The locator By.ID is invalid
D. time.sleep(10) may cause unnecessary delay or fail if element appears late

Solution

  1. Step 1: Analyze use of time.sleep(10)

    It pauses test for 10 seconds regardless of whether the 'finish' element is ready.
  2. Step 2: Understand impact on test reliability

    If element appears earlier, test wastes time; if later, test may fail after sleep ends.
  3. Final Answer:

    time.sleep(10) may cause unnecessary delay or fail if element appears late -> Option D
  4. Quick Check:

    Fixed sleep causes delay or flakiness = A [OK]
Hint: Avoid fixed sleeps; use waits that check conditions [OK]
Common Mistakes:
  • Assuming fixed sleep is always safe
  • Ignoring element readiness timing
  • Thinking locator or click usage is wrong
5. You want to wait for a dynamic element that may appear anytime within 15 seconds after clicking a button. Which approach is best to make your Selenium test fast and reliable?
hard
A. Use WebDriverWait(driver, 15).until(EC.presence_of_element_located(locator))
B. Use time.sleep(15) after clicking the button
C. Use a loop with time.sleep(1) checking element presence manually
D. Do not wait; try to interact immediately after clicking

Solution

  1. Step 1: Evaluate fixed sleep approach

    time.sleep(15) always waits full 15 seconds, slowing tests unnecessarily.
  2. Step 2: Evaluate proper wait approach

    WebDriverWait waits up to 15 seconds but continues as soon as element appears, making tests faster and stable.
  3. Step 3: Consider manual loop and no wait

    Manual loops are error-prone and less efficient; no wait risks failures.
  4. Final Answer:

    Use WebDriverWait(driver, 15).until(EC.presence_of_element_located(locator)) -> Option A
  5. Quick Check:

    Proper wait with timeout = B [OK]
Hint: Use WebDriverWait with condition, avoid fixed sleep [OK]
Common Mistakes:
  • Using fixed sleep wasting time
  • Writing manual loops instead of waits
  • Skipping waits causing flaky tests
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard