Sometimes web elements are hidden and not visible on the page. We need to handle them carefully to avoid test failures.
Handling hidden elements in Selenium Java
WebElement element = driver.findElement(By.id("elementId"));
boolean isDisplayed = element.isDisplayed();isDisplayed() checks if the element is visible on the page.
Hidden elements exist in the DOM but return false for isDisplayed().
WebElement hiddenElement = driver.findElement(By.id("hiddenElement"));
boolean visible = hiddenElement.isDisplayed();if (!hiddenElement.isDisplayed()) { System.out.println("Element is hidden, cannot click."); }
JavascriptExecutor js = (JavascriptExecutor) driver;
js.executeScript("arguments[0].click();", hiddenElement);This test opens a page, finds a button that might be hidden, checks if it is visible, and clicks it normally if visible. If hidden, it clicks using JavaScript to avoid errors.
import org.openqa.selenium.By; import org.openqa.selenium.WebDriver; import org.openqa.selenium.WebElement; import org.openqa.selenium.chrome.ChromeDriver; import org.openqa.selenium.JavascriptExecutor; public class HiddenElementTest { public static void main(String[] args) { System.setProperty("webdriver.chrome.driver", "path/to/chromedriver"); WebDriver driver = new ChromeDriver(); try { driver.get("https://example.com/hidden-element-page"); WebElement hiddenButton = driver.findElement(By.id("hiddenButton")); if (hiddenButton.isDisplayed()) { hiddenButton.click(); System.out.println("Clicked the visible button."); } else { System.out.println("Button is hidden, clicking with JavaScript."); JavascriptExecutor js = (JavascriptExecutor) driver; js.executeScript("arguments[0].click();", hiddenButton); System.out.println("Clicked the hidden button via JavaScript."); } } finally { driver.quit(); } } }
Always prefer normal clicks over JavaScript clicks for better test reliability.
Hidden elements can cause exceptions if you try to interact with them directly.
Use isDisplayed() to check visibility before actions.
Hidden elements exist in the page but are not visible.
Use isDisplayed() to check if an element is visible.
Use JavaScript clicks only if normal clicks fail due to hidden elements.
Practice
Solution
Step 1: Understand visibility check methods
TheisDisplayed()method returns true if the element is visible on the page.Step 2: Compare other methods
isEnabled()checks if element is enabled,getText()gets text, andclick()performs click action, none check visibility.Final Answer:
isDisplayed() -> Option BQuick Check:
Visibility check = isDisplayed() [OK]
- Confusing isEnabled() with visibility check
- Using getText() to check visibility
- Trying to click before checking visibility
Solution
Step 1: Cast driver to JavascriptExecutor
In Selenium Java, to run JavaScript, cast WebDriver to JavascriptExecutor: ((JavascriptExecutor)driver).Step 2: Use executeScript method correctly
Call executeScript with JavaScript code and element as argument: executeScript("arguments[0].click();", element);Final Answer:
((JavascriptExecutor)driver).executeScript("arguments[0].click();", element); -> Option DQuick Check:
Correct JS click syntax = ((JavascriptExecutor)driver).executeScript("arguments[0].click();", element); [OK]
- Calling executeScript directly on driver without casting
- Using wrong method names like execute()
- Incorrect object references for JavascriptExecutor
WebElement element = driver.findElement(By.id("hiddenBtn"));
boolean visible = element.isDisplayed();
System.out.println(visible);Solution
Step 1: Understand isDisplayed() behavior
TheisDisplayed()method returns false if the element exists but is hidden (not visible).Step 2: Check element presence
SincefindElementfinds the element by id, no exception is thrown. The element is hidden, soisDisplayed()returns false.Final Answer:
false -> Option AQuick Check:
Hidden element visibility = false [OK]
- Assuming isDisplayed() throws exception if element is hidden
- Confusing false with null
- Expecting true without checking visibility
WebElement element = driver.findElement(By.id("hiddenBtn"));
element.click();Solution
Step 1: Understand click behavior on hidden elements
Trying to click a hidden element usingelement.click()causesElementNotVisibleExceptionbecause Selenium cannot interact with invisible elements.Step 2: Check element presence
The element is found, so noNoSuchElementException. It is not stale, so noStaleElementReferenceException.Final Answer:
Throws ElementNotVisibleException because element is hidden -> Option CQuick Check:
Click hidden element error = ElementNotVisibleException [OK]
- Assuming click works on hidden elements
- Confusing NoSuchElementException with visibility issues
- Ignoring exception types for stale elements
Solution
Step 1: Scroll element into view using JavaScript
Hidden elements due to scrolling can be made visible by scrolling them into view withJavascriptExecutor:executeScript("arguments[0].scrollIntoView(true);", element);Step 2: Click the now visible element
After scrolling, useelement.click()to interact safely with the visible element.Final Answer:
Use JavascriptExecutor to scroll element into view, then use element.click() -> Option AQuick Check:
Scroll then click = JavascriptExecutor + click [OK]
- Clicking without scrolling causes exceptions
- Using Thread.sleep() instead of scrolling
- Relying only on Actions class without scrolling
Ready to prove it?
Pass the test and this pattern moves unknown
