How to click on hidden element in Selenium WebDriver?

SeleniumAutomation TestingTesting Tools

We can click on the hidden element in Selenium webdriver. In DOM, the hidden elements are the ones which are not displayed on the page. CSS property style should have the value display:none set for the hidden elements. Also, if the hidden element resides within a form tag, it can be made hidden by setting the attribute type to hidden.

ElementNotVisibleException is thrown by Selenium while handling hidden elements. So JavaScript Executors can be utilized to access and handle these elements. The executeScript method is used in Selenium to execute the JavaScript commands. The commands to be executed are passed as parameters to that method.

Let us take a scenario in which initially there are two buttons − Hide and Show along with an edit box. As the Hide button is clicked, the below edit button becomes hidden on the page.

On clicking Hide, the page becomes −

Let us make an attempt to enter some text within the hidden edit box. To start with, we will locate a hidden edit box with the getElementById method and utilize the method value to input some text into it.


executor.executeScript ("document.getElementById('id').value='QA';");


import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.JavascriptExecutor;
public class ElementHiddenClk{
   public static void main(String[] args) {
      WebDriver driver = new ChromeDriver();
      driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);
      // identify element
      // Javascript executor class
      JavascriptExecutor js = (JavascriptExecutor) driver;
      // identify element and set value
      String str = (String) js.executeScript("return       document.getElementById('displayed−text').value");
      System.out.print("Value entered in hidden element: " +str);


Published on 01-Feb-2021 16:35:59