How to click on hidden element in Selenium WebDriver?
Last Updated :
18 Oct, 2024
In Selenium WebDriver, interacting with web elements is key to automating web applications. However, certain elements on a webpage might be hidden or obscured, making it challenging to interact with them directly. In such cases, developers and testers often need to use alternative methods, such as JavaScriptExecutor in Selenium, to click on these hidden elements.
This ensures that even elements that are not directly visible can be interacted with, enhancing the flexibility of test automation. In this guide, we will explore how to click on hidden elements in Selenium WebDriver using simple techniques
Understanding Hidden Elements in Selenium
Selenium WebDriver cannot see a hidden element on the web page but the implementation is present in the DOM structure. These elements could be styled as hidden, positioned off-screen or present in a collapsed. When you inspect these hidden elements you will find their attribute as visibility:hidden or display:none.
Following a sample code of a hidden element -
Sample code of a hidden elementIn the above sample code, you can see display is set to none. Hence, that element will not be visible on the web page and when you try to fetch such an element, WebDriver will return ElementNotVisible Exception.
How to click hidden elements in Selenium WebDriver?
Interacting with hidden elements in Selenium WebDriver can be challenging since standard actions like clicking or sending keys are generally not allowed on elements that are not visible. However, there are a few strategies you can use to interact with these elements.
1. Using JavaScript Executor
JavaScript works well to make element visible, it can manipulate the DOM directly. With the help of JavascriptExecutor we can interact with hidden elements and perform click. This approach is one of the common approach because it bypasses the visibility check.
ClickHiddenElement.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 java.util.concurrent.TimeUnit;
public class ClickHiddenElement {
public static void main(String[] args) {
// Set up ChromeDriver path (could be replaced with WebDriverManager for better flexibility)
System.setProperty("webdriver.chrome.driver", "D:\\chromedriver-win64\\chromedriver-win64\\chromedriver.exe");
// Initialize WebDriver
WebDriver driver = new ChromeDriver();
// Maximize browser window
driver.manage().window().maximize();
try {
// Step 1: Navigate to the target website
driver.get("https://www.letskodeit.com/practice");
// Step 2: Implicit wait to ensure elements load within 5 seconds
driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);
// Step 3: Locate the hidden element using its ID
WebElement hiddenElement = driver.findElement(By.id("displayed-text"));
// Step 4: Use JavaScriptExecutor to click on the hidden element
JavascriptExecutor js = (JavascriptExecutor) driver;
js.executeScript("arguments[0].click();", hiddenElement);
// Confirmation message
System.out.println("Hidden element clicked successfully!");
} catch (Exception e) {
// Handle any exceptions that occur during execution
System.out.println("An error occurred: " + e.getMessage());
} finally {
// Step 5: Close the browser after execution
driver.quit();
}
}
}
Output
click_hidden output2. Changing the Element's Style
Another approach involves modifying the hidden element's style to make it visible before performing the click. This can be done by changing the CSS properties using JavaScript.
Webment hiddenElement = driver.findElement(By.id("hiddenElementId"));
// Make the element visible first
JavascriptExecutor jsExecutor = (JavascriptExecutor) driver;
jsExecutor.executeScript("arguments[0].style.display='block';", hiddenElement);
// Click on the element
hiddenElement.click();
Conclusion
Clicking on hidden elements in Selenium WebDriver can be effectively achieved by using JavaScriptExecutor, which bypasses the limitations of traditional WebDriver methods. This technique is particularly useful when elements are intentionally hidden for design or dynamic loading purposes.
By leveraging this approach, you can ensure robust and comprehensive automation of web applications, regardless of hidden elements. Integrating this method in your tests can significantly improve the flexibility and coverage of your Selenium automation framework.
Similar Reads
How to Force Selenium WebDriver to Click on Element which is Not Currently Visible?
A programming language that is used to execute a set of instructions that a computer can understand and execute to perform various tasks is known as Java. Java can be controlled autonomously using various automation tools. Table of Content Visibility criteriaWait for VisibilityScroll the Element int
7 min read
How to check if an element exists with Python Selenium?
Selenium is one of the most powerful and widely used tools for web automating web applications. Whether you're a software developer or a QA tester, Selenium is an important tool to have in your toolkit. Selenium is widely used for automating user interactions like clicking buttons, filling out the f
7 min read
How to Get Child Element in Selenium?
The tool that reduces human effort and makes the work relatively easier by automating the browser is known as Selenium. The elements which are located within some other elements, such elements are called child elements. Sometimes, we have to handle such elements, such as values of radio buttons, lis
5 min read
How to check that the element is clickable or not in Java Selenium WebDriver?
Ensuring that an element is clickable in your Selenium WebDriver tests is crucial for validating the functionality and user experience of your web applications. In Java Selenium WebDriver, checking if an element is clickable before interacting with it can help avoid errors and ensure that your test
3 min read
How to Fix Selenium's "Element Is Not Clickable at Point"
When automating web applications with Selenium WebDriver, you may encounter various exceptions that can halt your tests. One common issue that many testers face is the "Element is not clickable at point" exception. This error can be frustrating, especially when you're trying to interact with an elem
6 min read
How to hide Firefox window (Selenium WebDriver)?
In situations where you might wish to execute tests without a visible browser window, while automating web tests with Selenium WebDriver; it is possible for one to run headless browsers for example which will help save resources and speed up the execution of these tests. This article will cover how
3 min read
How to Click on a Hyperlink Using Java Selenium WebDriver?
An open-source tool that is used to automate the browser is known as Selenium. Automation reduces human effort and makes the work comparatively easier. There are numerous circumstances in which the user wants to open a new page or perform a certain action with the click of the hyperlink. In this art
4 min read
How to use xPath in Selenium WebDriver to grab SVG elements?
Selenium WebDriver is a powerful tool for automating web browsers to test the functionality of web applications. However, when working with SVG elements on a webpage, it can be tricky to locate and interact with them using standard locators like ID, class, or name. In such cases, XPath comes to the
2 min read
click() element method - Selenium Python
Seleniumâs Python Module is built to perform automated testing with Python. Selenium Python bindings provides a simple API to write functional/acceptance tests using Selenium WebDriver. To open a webpage using Selenium Python, checkout - Navigating links using get method â Selenium Python . Just bei
2 min read
How to Drag and Drop an Element using Selenium WebDriver in Java?
Selenium is an open-source web automation tool that supports many user actions to perform in the web browser. Automating a modern web page that has a drag and drop functionality and drag and drop is used to upload the files and so many user activities. so to perform the drag and drop actions the sel
2 min read