Hint: Use WebDriverWait(driver, time).until(EC.condition(locator)) [OK]
Common Mistakes:
Using incorrect method names like wait_for_clickable
Passing locator as separate arguments instead of tuple
Misplacing driver or timeout parameters
3. What will be the output of the following Selenium Python code snippet?
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, 5)
element = wait.until(EC.presence_of_element_located((By.CSS_SELECTOR, '.btn-primary')))
print(element.text)
medium
A. Raises TimeoutException immediately
B. Prints the text of the first element with class 'btn-primary' if found within 5 seconds
C. Prints None
D. Prints the number of elements with class 'btn-primary'
Solution
Step 1: Understand WebDriverWait with presence_of_element_located
The wait.until waits up to 5 seconds for the element with CSS class 'btn-primary' to appear in the DOM.
Step 2: Check what print(element.text) does
If found, element.text prints the visible text of that element.
Final Answer:
Prints the text of the first element with class 'btn-primary' if found within 5 seconds -> Option B
Quick Check:
Wait for presence then print text = Prints the text of the first element with class 'btn-primary' if found within 5 seconds [OK]
Hint: Wait returns element; print its text to see content [OK]
Common Mistakes:
Confusing presence_of_element_located with visibility
Expecting count instead of element text
Assuming immediate exception without wait
4. Identify the error in this Selenium Python code snippet and choose the correct fix:
element = driver.find_element(By.ID, 'login')
element.click()
Assume the element appears after some delay.
medium
A. Remove the click() call
B. Change By.ID to By.CLASS_NAME
C. Use driver.find_elements instead of find_element
D. Add WebDriverWait to wait until element is clickable before clicking
Solution
Step 1: Recognize timing issue in element interaction
The element may not be ready immediately, so clicking right away can cause errors.
Step 2: Use WebDriverWait to wait for clickability
Waiting until the element is clickable ensures the element is ready for interaction.
Final Answer:
Add WebDriverWait to wait until element is clickable before clicking -> Option D
Quick Check:
Wait for clickable before click = Add WebDriverWait to wait until element is clickable before clicking [OK]
Hint: Always wait for element to be clickable before clicking [OK]
Common Mistakes:
Changing locator type without reason
Removing click call disables test action
Using find_elements returns list, not single element
5. You want to create a reusable Selenium Python function that waits for an element to be visible and then clicks it. Which advanced pattern best solves this real challenge?
hard
A. Create a custom wrapper method combining WebDriverWait and click action
B. Use time.sleep() before every click to wait fixed time
C. Directly call driver.find_element().click() without waiting
D. Write separate wait and click calls every time in test code
Solution
Step 1: Identify need for reusable, reliable click action
Waiting for visibility and clicking is a common pattern that benefits from reuse.
Step 2: Use a wrapper method to combine wait and click
A custom method hides complexity and improves test code readability and reliability.
Final Answer:
Create a custom wrapper method combining WebDriverWait and click action -> Option A
Quick Check:
Reusable wrapper method solves timing and reuse [OK]
Hint: Wrap wait and click in one function for reuse [OK]
Common Mistakes:
Using fixed sleep wastes time and is unreliable
Skipping wait causes flaky tests
Repeating wait and click code everywhere reduces maintainability
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.