Bird
Raised Fist0
Selenium Javatesting~8 mins

Handling hidden elements in Selenium Java - Framework Patterns

Choose your learning style10 modes available
Framework Mode - Handling hidden elements
Folder Structure
src/
└── test/
    └── java/
        └── com/
            └── example/
                ├── pages/
                │   ├── LoginPage.java
                │   ├── DashboardPage.java
                ├── tests/
                │   ├── LoginTest.java
                │   ├── HiddenElementTest.java
                ├── utils/
                │   ├── WaitUtils.java
                │   ├── DriverFactory.java
                └── config/
                    └── TestConfig.java
Test Framework Layers
  • Driver Layer: Manages WebDriver setup and teardown (e.g., DriverFactory.java).
  • Page Objects: Encapsulate page elements and actions (e.g., LoginPage.java). Includes methods to handle hidden elements using explicit waits and JavaScript.
  • Tests: Test classes that use page objects to perform test scenarios (e.g., HiddenElementTest.java).
  • Utilities: Helper classes for waits, JavaScript execution, and common actions (e.g., WaitUtils.java).
  • Configuration: Holds environment settings, browser options, and credentials (e.g., TestConfig.java).
Configuration Patterns
  • Environment Properties: Use property files or Java classes (TestConfig.java) to store URLs, credentials, and environment-specific data.
  • Browser Settings: Configure browser type and options in DriverFactory.java, allowing tests to run on different browsers.
  • Handling Hidden Elements: Use explicit waits (WebDriverWait) with ExpectedConditions.visibilityOfElementLocated or JavaScript to interact with hidden elements.
  • Credentials Management: Store sensitive data securely, e.g., environment variables or encrypted files, accessed via config classes.
Test Reporting and CI/CD Integration
  • TestNG Reports: Use TestNG's built-in HTML reports for test execution results.
  • Logging: Integrate logging (e.g., Log4j) to capture detailed test steps and hidden element handling attempts.
  • CI/CD Integration: Configure Jenkins or GitHub Actions to run tests on code commits, with reports archived and notifications sent on failures.
  • Screenshot on Failure: Capture screenshots when hidden element interactions fail to help debugging.
Best Practices for Handling Hidden Elements
  1. Use Explicit Waits: Always wait for elements to be visible or clickable before interacting to avoid stale or hidden element errors.
  2. JavaScript Executor: When elements are hidden but need interaction, use JavaScript to click or set values safely.
  3. Page Object Methods: Encapsulate hidden element handling inside page object methods to keep tests clean and reusable.
  4. Avoid Thread.sleep(): Never use fixed waits; rely on dynamic waits to improve test speed and reliability.
  5. Clear Error Messages: Provide meaningful exceptions or logs when hidden element interactions fail to ease troubleshooting.
Self Check

Where in this folder structure would you add a new method to safely click a hidden button on the Dashboard page?

Key Result
Use explicit waits and JavaScript in page objects to handle hidden elements safely.

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