Bird
Raised Fist0
Selenium Javatesting~20 mins

Handling hidden elements in Selenium Java - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Hidden Element Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate
2:00remaining
What is the output of this Selenium Java code snippet?
Consider the following Selenium Java code that tries to click a hidden button. What will happen when this code runs?
Selenium Java
WebElement hiddenButton = driver.findElement(By.id("hiddenBtn"));
hiddenButton.click();
System.out.println("Clicked hidden button");
AThrows ElementNotInteractableException at click()
BPrints 'Clicked hidden button' without error
CThrows NoSuchElementException at findElement()
DThrows TimeoutException waiting for element
Attempts:
2 left
💡 Hint
Hidden elements cannot be clicked directly by Selenium WebDriver.
❓ assertion
intermediate
2:00remaining
Which assertion correctly verifies an element is hidden?
You want to assert that a web element with id 'secret' is hidden on the page. Which assertion is correct?
AassertEquals(driver.findElement(By.id("secret")).getText(), "");
BassertTrue(driver.findElement(By.id("secret")).isDisplayed());
CassertNull(driver.findElement(By.id("secret")));
DassertFalse(driver.findElement(By.id("secret")).isDisplayed());
Attempts:
2 left
💡 Hint
Use isDisplayed() to check visibility.
🔧 Debug
advanced
2:00remaining
Why does this Selenium code fail to click a hidden element?
This code tries to click a hidden element but fails. Identify the reason.
Selenium Java
WebElement hidden = driver.findElement(By.cssSelector(".hidden-class"));
hidden.click();
AElement is present but not visible, so click() throws ElementNotInteractableException
BSelector is invalid, causing NoSuchElementException
CPage did not load fully, causing StaleElementReferenceException
DElement is disabled, causing InvalidElementStateException
Attempts:
2 left
💡 Hint
Hidden elements cannot be clicked directly.
❓ framework
advanced
2:00remaining
Which Selenium approach handles clicking hidden elements safely?
You need to click a hidden element reliably in Selenium Java. Which approach is best?
AUse WebDriverWait until element is visible then click()
BUse Actions class to move to element then click()
CUse JavaScriptExecutor to click the element via JS
DUse findElements and click the first element
Attempts:
2 left
💡 Hint
JS can click elements even if hidden.
🧠 Conceptual
expert
2:00remaining
What is the main risk of clicking hidden elements in automated tests?
Why should automated tests avoid clicking hidden elements directly?
AIt prevents the page from loading further
BIt may cause tests to pass incorrectly by interacting with elements users cannot see
CIt always causes Selenium to crash the browser
DIt causes the element to become visible automatically
Attempts:
2 left
💡 Hint
Think about user experience vs test behavior.

Practice

(1/5)
1. Which Selenium method helps to check if an element is visible on the page?
easy
A. isEnabled()
B. isDisplayed()
C. getText()
D. click()

Solution

  1. Step 1: Understand visibility check methods

    The isDisplayed() method returns true if the element is visible on the page.
  2. Step 2: Compare other methods

    isEnabled() checks if element is enabled, getText() gets text, and click() performs click action, none check visibility.
  3. Final Answer:

    isDisplayed() -> Option B
  4. Quick Check:

    Visibility check = isDisplayed() [OK]
Hint: Use isDisplayed() to check visibility quickly [OK]
Common Mistakes:
  • Confusing isEnabled() with visibility check
  • Using getText() to check visibility
  • Trying to click before checking visibility
2. Which of the following is the correct syntax to click a hidden element using JavaScript in Selenium Java?
easy
A. driver.javascriptExecutor.execute("arguments[0].click();", element);
B. driver.executeScript("arguments[0].click();", element);
C. JavascriptExecutor.executeScript("arguments[0].click();", element);
D. ((JavascriptExecutor)driver).executeScript("arguments[0].click();", element);

Solution

  1. Step 1: Cast driver to JavascriptExecutor

    In Selenium Java, to run JavaScript, cast WebDriver to JavascriptExecutor: ((JavascriptExecutor)driver).
  2. Step 2: Use executeScript method correctly

    Call executeScript with JavaScript code and element as argument: executeScript("arguments[0].click();", element);
  3. Final Answer:

    ((JavascriptExecutor)driver).executeScript("arguments[0].click();", element); -> Option D
  4. Quick Check:

    Correct JS click syntax = ((JavascriptExecutor)driver).executeScript("arguments[0].click();", element); [OK]
Hint: Cast driver to JavascriptExecutor before executeScript [OK]
Common Mistakes:
  • Calling executeScript directly on driver without casting
  • Using wrong method names like execute()
  • Incorrect object references for JavascriptExecutor
3. What will be the output of the following code snippet if the element is hidden?
WebElement element = driver.findElement(By.id("hiddenBtn"));
boolean visible = element.isDisplayed();
System.out.println(visible);
medium
A. false
B. true
C. null
D. Throws NoSuchElementException

Solution

  1. Step 1: Understand isDisplayed() behavior

    The isDisplayed() method returns false if the element exists but is hidden (not visible).
  2. Step 2: Check element presence

    Since findElement finds the element by id, no exception is thrown. The element is hidden, so isDisplayed() returns false.
  3. Final Answer:

    false -> Option A
  4. Quick Check:

    Hidden element visibility = false [OK]
Hint: isDisplayed() returns false for hidden elements [OK]
Common Mistakes:
  • Assuming isDisplayed() throws exception if element is hidden
  • Confusing false with null
  • Expecting true without checking visibility
4. Identify the error in this code snippet that tries to click a hidden element:
WebElement element = driver.findElement(By.id("hiddenBtn"));
element.click();
medium
A. No error, element.click() works on hidden elements
B. Throws NoSuchElementException because element is missing
C. Throws ElementNotVisibleException because element is hidden
D. Throws StaleElementReferenceException due to stale element

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]
Hint: Clicking hidden elements throws ElementNotVisibleException [OK]
Common Mistakes:
  • Assuming click works on hidden elements
  • Confusing NoSuchElementException with visibility issues
  • Ignoring exception types for stale elements
5. You want to click a button that is hidden by default and only visible after scrolling. Which approach is best to handle this in Selenium Java?
hard
A. Use JavascriptExecutor to scroll element into view, then use element.click()
B. Directly call element.click() without scrolling
C. Use Thread.sleep() to wait and then click
D. Use Actions class to move to element and then click

Solution

  1. Step 1: Scroll element into view using JavaScript

    Hidden elements due to scrolling can be made visible by scrolling them into view with JavascriptExecutor: executeScript("arguments[0].scrollIntoView(true);", element);
  2. Step 2: Click the now visible element

    After scrolling, use element.click() to interact safely with the visible element.
  3. Final Answer:

    Use JavascriptExecutor to scroll element into view, then use element.click() -> Option A
  4. Quick Check:

    Scroll then click = JavascriptExecutor + click [OK]
Hint: Scroll element into view before clicking hidden buttons [OK]
Common Mistakes:
  • Clicking without scrolling causes exceptions
  • Using Thread.sleep() instead of scrolling
  • Relying only on Actions class without scrolling
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