- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
How to click on hidden element in Selenium WebDriver?
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.
Syntax
executor.executeScript ("document.getElementById('id').value='QA';");
Example
import org.openqa.selenium.By; import org.openqa.selenium.WebDriver; import org.openqa.selenium.WebElement; import org.openqa.selenium.chrome.ChromeDriver; import java.util.concurrent.TimeUnit; import org.openqa.selenium.JavascriptExecutor; public class ElementHiddenClk{ public static void main(String[] args) { System.setProperty("webdriver.chrome.driver", "C:\Users\ghs6kor\Desktop\Java\chromedriver.exe"); WebDriver driver = new ChromeDriver(); driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS); driver.get("https://learn.letskodeit.com/p/practice"); // identify element driver.findElement(By.id("hide−textbox")).click(); // Javascript executor class JavascriptExecutor js = (JavascriptExecutor) driver; // identify element and set value js.executeScript ("document.getElementById('displayed−text').value='QA';"); String str = (String) js.executeScript("return document.getElementById('displayed−text').value"); System.out.print("Value entered in hidden element: " +str); driver.close() } }
Output
- Related Articles
- How do you click on an element which is hidden using Selenium WebDriver?
- How to click on image in selenium webdriver Python?
- How to click on across browsers using Selenium Webdriver?
- How to click on a link using Selenium webdriver in Python.
- How to click Allow on Show Notifications popup using Selenium Webdriver?
- How to interact with hidden elements in Selenium Webdriver?
- How to perform double click on an element in Selenium?
- Selenium Webdriver submit() vs click().
- How to use Selenium webdriver to click google search?
- How to perform right click on an element with Actions in Selenium?
- How to perform double click on an element in Selenium with python?
- How to perform right click on an element in Selenium with python?
- How to click on a link in Selenium?
- How to use JavaScript in Selenium to click an Element?
- How to Resolve Stale Element Reference Exception in Selenium WebDriver?
