0
0
Selenium Javatesting~10 mins

Handling hidden elements in Selenium Java - Test Execution Trace

Choose your learning style9 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.