How to get the text of a hidden element in Selenium?

Earlier, we looked at how to get a visible text of an element in Selenium. And in this post, we will look at how we can get the text of a hidden element in Selenium.

Let’s have a look at an element whose text is hidden. You can find the element on our selenium playground website.

Hidden text element

The text is not visible on the webpage because it is hidden. Let’s try to get the hidden text using the getText() method.

We will get the element by using the findElement method. We can use the id attribute to find it.

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
		driver.get("https://testkru.com/Elements/TextMessages");

		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?

We can use the textContent property to get the hidden text of an element, and we can get this property value by using one of the below ways.

Let’s look at them one by one.

Using getAtrribute() Method

getAttribute() is used to get an element’s attribute or property value. You can read about the getAttribute() method in this article, where we have discussed it in detail.

We now have to get the textContent property value using the getAttribute() method, and we will have our hidden text value for that element.

The below code will help us achieve just that.

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
		driver.get("https://testkru.com/Elements/TextMessages");

		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 too.

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.

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
		driver.get("https://testkru.com/Elements/TextMessages");

		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

We again got the hidden text value of the element.

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.

Related Articles

Liked the article? Share this on

Leave a Comment

Your email address will not be published. Required fields are marked *