When working with Selenium automation, you might encounter scenarios where elements on the webpage have text that’s not directly visible in the browser but is still present in the DOM (Document Object Model). Accessing this hidden text can be important for validating data, debugging, or ensuring complete test coverage.
In this article, we’ll explain why hidden text appears, when you might need it, and show practical ways to extract it using Selenium WebDriver in Java.
Note: We have also attached a video at the end of this article. In case you are more comfortable with the video version, then please feel free to have a look
How Selenium normally retrieves text
In Selenium, the usual way to get the visible text of an element is by using the getText() method. So, let’s try to use that in an example.
We’ve highlighted the element in the image below. As you can see, its text isn’t visible on the page. You can check this element yourself on our Selenium playground website.

We will get the element by using the findElement method. We can use the id attribute to find it and then use the getText() method.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 | public class CodekruTest { @Test public void test() { // pass the path of the chromedriver location in the second argument System.setProperty( "webdriver.chrome.driver" , "/usr/local/bin/chromedriver" ); WebDriver driver = new ChromeDriver(); // opening the url WebElement element = driver.findElement(By.id( "hiddenText" )); System.out.println( "Inner Text of the element: " + element.getText()); } } |
Output –
Inner Text of the element:
We can see here that an empty string is returned. But then, how will we find the hidden text of an element?
How to get hidden text
To extract hidden text, we need to bypass the standard getText()
and directly access the element’s HTML attributes or its innerHTML or textContent.
Every HTML element has properties like textContent that include all text, visible or hidden. So, if we retrieve the value of the textContent property, then we will have our hidden text as well.
So, there are two ways to get the value of the “textContent” property in Selenium.
- Using getAttribute() Method
- And by using JavascriptExecutor
Let’s look at them one by one.
Using getAtrribute() Method
The getAttribute() method is used to retrieve an element’s attribute or property value. You can read more about how it works in detail in this article.
In our case, we want to get the value of the textContent property using the getAttribute() method. This will allow us to extract the hidden text from the element.
Here’s a sample code snippet that demonstrates how to do this:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 | public class CodekruTest { @Test public void test() { // pass the path of the chromedriver location in the second argument System.setProperty( "webdriver.chrome.driver" , "/usr/local/bin/chromedriver" ); WebDriver driver = new ChromeDriver(); // opening the url WebElement element = driver.findElement(By.id( "hiddenText" )); System.out.println( "Inner Text of the element: " + element.getAttribute( "textContent" )); } } |
Output –
Inner Text of the element: A Hidden Text
Finally, we got the hidden text value for our element. We can also achieve the same functionality using the JavascriptExecutor.
Using JavascriptExecutor
JavascriptExecutor helps execute the javascript code in Selenium. We can get the textContent attribute’s value by executing the script below.
return arguments[0].textContent
where arguments[0] is the element whose hidden text, we want to find.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 | public class CodekruTest { @Test public void test() { // pass the path of the chromedriver location in the second argument System.setProperty( "webdriver.chrome.driver" , "/usr/local/bin/chromedriver" ); WebDriver driver = new ChromeDriver(); // opening the url WebElement element = driver.findElement(By.id( "hiddenText" )); JavascriptExecutor jse = (JavascriptExecutor) driver; System.out.println(jse.executeScript( "return arguments[0].textContent" , element)); } } |
Output –
A Hidden Text
Video Tutorial
This is it. We hope that you have liked the article. If you have any doubts or concerns, please feel free to write us in the comments or mail us at admin@codekru.com.