How to get the text of an element in selenium?

This post will discuss several ways we can use to get the text of an element in Selenium. Please note that here we are talking about the visible text only, not the hidden text. ( Please look at this article to know how to get the hidden text of an element).

The visible text means the text that is visible on the webpage and not hidden using any CSS property.

<p>
 This is a text
</p>

Here, “This is a text” is visible on the webpage.

<p style="visibility: hidden;">
 This is also a text
</p>

The text is not visible on the webpage; thus, the methods we will explore in this post are irrelevant.

Below are some of the ways to get the text of an element and its sub-elements –

Let’s discuss all of them one by one.

Using getText() method

We have already written a post on the getText() method explaining it in detail. getText() method get the visible text of an element. It will also get the text of sub-elements if present.

We will get the text of the highlighted element in the below image. You can find the element on our playground website ( testkru.com/Elements/TextMessages )

Text element
  • We will first find the element using the findElement method. We can use the id attribute to find the element. Here the id of the element is “plainText”.
  • We can then use the getText() method to get the element’s text.
public class CodekruTest {

	@Test
	public void test() {

		// pass the path of the chromedriver location in the second argument
		System.setProperty("webdriver.chrome.driver", "C:\\Users\\MEHUL\\OneDrive\\Desktop\\chromedriver.exe");
		WebDriver driver = new ChromeDriver();

		// opening the url
		driver.get("https://testkru.com/Elements/TextMessages");

		WebElement element = driver.findElement(By.id("plainText"));
		System.out.println(element.getText());
		
	}
}

Output –

A Plain Text

Using getAttribute() method

getAttribute() helps get the attribute’s or the property’s value of an element. You can read about the getAttribute() method on this link.

We have one property, “innerText“, which also returns the visible text of an element. So, we will only have to use the getAttribute("innerText") to get the text of an element.

public class CodekruTest {

	@Test
	public void test() {

		// pass the path of the chromedriver location in the second argument
		System.setProperty("webdriver.chrome.driver", "C:\\Users\\MEHUL\\OneDrive\\Desktop\\chromedriver.exe");
		WebDriver driver = new ChromeDriver();

		// opening the url
		driver.get("https://testkru.com/Elements/TextMessages");

		WebElement element = driver.findElement(By.id("plainText"));
		System.out.println(element.getAttribute("innerText"));

	}
}

Output –

A Plain Text

We got the same result here as well.

Using JavascriptExecutor

JavascriptExecutor helps in executing the javascript code in Selenium. We can get the innerText property’s value by executing the script below.

return arguments[0].innerText

where arguments[0] is the element whose innerText we want to find.

Below is the whole code for executing the above script and getting the text of an element.

public class CodekruTest {

	@Test
	public void test() {

		// pass the path of the chromedriver location in the second argument
		System.setProperty("webdriver.chrome.driver", "C:\\Users\\MEHUL\\OneDrive\\Desktop\\chromedriver.exe");
		WebDriver driver = new ChromeDriver();

		// opening the url
		driver.get("https://testkru.com/Elements/TextMessages");

		WebElement element = driver.findElement(By.id("plainText"));

		JavascriptExecutor jse = (JavascriptExecutor) driver;
		System.out.println(jse.executeScript("return arguments[0].innerText", element));

	}
}

Output –

A Plain Text

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 [email protected]

Related Articles

Liked the article? Share this on

Leave a Comment

Your email address will not be published.