Bird
Raised Fist0
Selenium Javatesting~10 mins

Handling hidden elements in Selenium Java - Test Execution Trace

Choose your learning style10 modes available
Test Overview

This test checks if a hidden button can be found and clicked using Selenium in Java. It verifies that the button is not visible but still clickable by using JavaScript execution.

Test Code - JUnit with Selenium WebDriver
Selenium Java
import org.openqa.selenium.By;
import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.assertTrue;

public class HiddenElementTest {
    private WebDriver driver;

    @BeforeEach
    public void setUp() {
        driver = new ChromeDriver();
    }

    @AfterEach
    public void tearDown() {
        if (driver != null) {
            driver.quit();
        }
    }

    @Test
    public void testClickHiddenButton() {
        driver.get("https://example.com/hidden-button");

        // Locate the hidden button by id
        WebElement hiddenButton = driver.findElement(By.id("hidden-btn"));

        // Check that the button is not displayed
        assertTrue(!hiddenButton.isDisplayed(), "Button should be hidden");

        // Click the hidden button using JavaScript
        JavascriptExecutor js = (JavascriptExecutor) driver;
        js.executeScript("arguments[0].click();", hiddenButton);

        // Verify that clicking the button triggered expected change
        WebElement message = driver.findElement(By.id("message"));
        assertTrue(message.isDisplayed(), "Message should be visible after click");
        assertTrue(message.getText().contains("Clicked"), "Message text should confirm click");
    }
}
Execution Trace - 8 Steps
StepActionSystem StateAssertionResult
1Test starts and Chrome browser opensChrome browser window is open and ready—PASS
2Navigates to https://example.com/hidden-buttonPage with hidden button loaded—PASS
3Finds the hidden button element by id 'hidden-btn'Hidden button element located but not visiblehiddenButton.isDisplayed() returns falsePASS
4Asserts the button is hidden (not displayed)Button is confirmed hiddenassertTrue(!hiddenButton.isDisplayed()) passesPASS
5Clicks the hidden button using JavaScript executorButton click event triggered despite being hidden—PASS
6Finds the message element by id 'message'Message element is now visible on pagemessage.isDisplayed() returns truePASS
7Asserts the message is visible and contains 'Clicked'Message confirms button clickassertTrue(message.getText().contains("Clicked")) passesPASS
8Test ends and browser closesBrowser closed cleanly—PASS
Failure Scenario
Failing Condition: Hidden button element is not found or JavaScript click does not trigger expected action
Execution Trace Quiz - 3 Questions
Test your understanding
Why does the test use JavaScript to click the hidden button?
ABecause JavaScript is faster than Selenium click
BBecause the button is disabled
CBecause Selenium cannot click elements that are not visible
DBecause the button is inside an iframe
Key Result
When elements are hidden and cannot be clicked normally, using JavaScript execution to click them is a reliable way to test their functionality.

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