Bird
0
0

Identify the error in this code snippet that tries to click a hidden element:

medium📝 Debug Q14 of 15
Selenium Java - JavaScriptExecutor
Identify the error in this code snippet that tries to click a hidden element:
WebElement element = driver.findElement(By.id("hiddenBtn"));
element.click();
ANo error, element.click() works on hidden elements
BThrows NoSuchElementException because element is missing
CThrows ElementNotVisibleException because element is hidden
DThrows StaleElementReferenceException due to stale element
Step-by-Step Solution
Solution:
  1. Step 1: Understand click behavior on hidden elements

    Trying to click a hidden element using element.click() causes ElementNotVisibleException because Selenium cannot interact with invisible elements.
  2. Step 2: Check element presence

    The element is found, so no NoSuchElementException. It is not stale, so no StaleElementReferenceException.
  3. Final Answer:

    Throws ElementNotVisibleException because element is hidden -> Option C
  4. Quick Check:

    Click hidden element error = ElementNotVisibleException [OK]
Quick Trick: Clicking hidden elements throws ElementNotVisibleException [OK]
Common Mistakes:
MISTAKES
  • Assuming click works on hidden elements
  • Confusing NoSuchElementException with visibility issues
  • Ignoring exception types for stale elements

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Selenium Java Quizzes