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
button = WebDriverWait(driver, 10).until(
EC.element_to_be_clickable((By.ID, 'login-btn'))
)
button.click()
print('Clicked')
medium
A. Throws TimeoutException immediately
B. Prints 'Clicked' without clicking the button
C. Prints 'Clicked' after clicking the button if clickable
D. SyntaxError due to wrong import
Solution
Step 1: Analyze WebDriverWait usage
The code waits up to 10 seconds for the element with id 'login-btn' to be clickable.
Step 2: Understand click and print behavior
If clickable, it clicks the button and prints 'Clicked'. No syntax errors or immediate exceptions expected.
Final Answer:
Prints 'Clicked' after clicking the button if clickable -> Option C
Quick Check:
Wait for clickable then click = prints 'Clicked' [OK]
Hint: Wait ensures clickable before click, then prints [OK]
Common Mistakes:
Assuming immediate TimeoutException without wait
Thinking print runs before click
Confusing import statements as errors
4. You wrote this Selenium code to click a button:
But the test fails with ElementNotInteractableException. What is the likely cause?
medium
A. The button is hidden or disabled and cannot be clicked
B. The locator By.ID is incorrect syntax
C. The click() method is not supported on buttons
D. The driver object is not initialized
Solution
Step 1: Understand ElementNotInteractableException meaning
This exception means the element is found but cannot be interacted with, often because it is hidden or disabled.
Step 2: Evaluate other options
By.ID syntax is correct, click() works on buttons, and if driver was not initialized, a different error would occur.
Final Answer:
The button is hidden or disabled and cannot be clicked -> Option A
Quick Check:
ElementNotInteractable = element not clickable [OK]
Hint: ElementNotInteractable means element is hidden or disabled [OK]
Common Mistakes:
Confusing locator syntax with interaction errors
Thinking click() is unsupported on buttons
Assuming driver not initialized causes this error
5. You want to write a test scenario that types 'hello' into a text box only if it is visible and enabled. Which Selenium Python code snippet correctly achieves this?
hard
A. input_box = driver.find_element(By.ID, 'input')
if input_box.is_displayed() and input_box.is_enabled():
input_box.send_keys('hello')
B. input_box = driver.find_element(By.ID, 'input')
input_box.send_keys('hello')
C. input_box = driver.find_element(By.ID, 'input')
if input_box.is_displayed() or input_box.is_enabled():
input_box.send_keys('hello')
D. input_box = driver.find_element(By.ID, 'input')
if not input_box.is_displayed() and not input_box.is_enabled():
input_box.send_keys('hello')
Solution
Step 1: Check conditions for safe typing
We must ensure the input box is both visible (is_displayed) and enabled (is_enabled) before typing.
Step 2: Analyze each option's condition
input_box = driver.find_element(By.ID, 'input')
if input_box.is_displayed() and input_box.is_enabled():
input_box.send_keys('hello') uses 'and' correctly. input_box = driver.find_element(By.ID, 'input')
input_box.send_keys('hello') types without checks. input_box = driver.find_element(By.ID, 'input')
if input_box.is_displayed() or input_box.is_enabled():
input_box.send_keys('hello') uses 'or' which allows typing if only one condition is true. input_box = driver.find_element(By.ID, 'input')
if not input_box.is_displayed() and not input_box.is_enabled():
input_box.send_keys('hello') types only if both are false, which is wrong.
Final Answer:
input_box = driver.find_element(By.ID, 'input')
if input_box.is_displayed() and input_box.is_enabled():
input_box.send_keys('hello') -> Option A
Quick Check:
Use 'and' to check visible and enabled before typing [OK]
Hint: Check both visible and enabled with 'and' before send_keys [OK]
Common Mistakes:
Using 'or' instead of 'and' for conditions
Skipping visibility and enabled checks
Typing when element is hidden or disabled
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.