Which of the following is the best reason to avoid using time.sleep() in Selenium tests?
Think about what happens if the page loads faster than the sleep time.
time.sleep() pauses the test for a fixed time regardless of page state, which can slow tests unnecessarily. Proper waits wait only as long as needed.
What will be the output of this Selenium Python code snippet?
from selenium import webdriver from selenium.webdriver.common.by import By from selenium.webdriver.support.ui import WebDriverWait from selenium.webdriver.support import expected_conditions as EC class DummyDriver: def find_element(self, by, value): raise Exception('Element not found') try: driver = DummyDriver() wait = WebDriverWait(driver, 3) wait.until(EC.presence_of_element_located((By.ID, 'myid'))) print('Element found') except Exception as e: print(f'Error: {e}')
Consider what happens when find_element always raises an exception.
The dummy driver always raises an exception when searching for the element, so the wait fails immediately and prints the error message.
Which assertion correctly verifies that an element is visible after using an explicit wait in Selenium?
from selenium.webdriver.support.ui import WebDriverWait from selenium.webdriver.support import expected_conditions as EC from selenium.webdriver.common.by import By wait = WebDriverWait(driver, 10) wait.until(EC.visibility_of_element_located((By.CSS_SELECTOR, '.my-class'))) # Which assertion is correct here?
Visibility means the element is shown on the page.
The is_displayed() method returns True if the element is visible. Other options check different properties or are invalid.
What is the main problem with this Selenium Python code snippet?
import time from selenium.webdriver.support.ui import WebDriverWait from selenium.webdriver.support import expected_conditions as EC from selenium.webdriver.common.by import By wait = WebDriverWait(driver, 5) wait.until(EC.presence_of_element_located((By.ID, 'submit-btn'))) time.sleep(3) driver.find_element(By.ID, 'submit-btn').click()
Think about the purpose of explicit waits and fixed sleeps.
The explicit wait already waits for the element presence. Adding time.sleep(3) after causes a fixed delay that slows the test without benefit.
In a Selenium test framework, which approach best improves test reliability and speed when waiting for elements?
Consider how to wait efficiently and reliably for elements.
Explicit waits with Expected Conditions wait only as long as needed for specific elements, improving speed and reliability. Fixed sleeps waste time, implicit waits can cause unpredictable delays, and manual retries are inefficient.