Next-Gen App & Browser Testing Cloud
Trusted by 2 Mn+ QAs & Devs to accelerate their release cycles

You can get the text of a web element in Selenium by locating the element and calling the getText() method of the WebElement interface. It returns the visible, rendered text between the element's tags, including the text of its child elements, with leading and trailing whitespace trimmed. For input fields or hidden text, use getAttribute() instead.
In Selenium, getText() reads only what is actually rendered on screen, not hidden attributes or script-generated values. This makes it the go-to method for validating labels, headings, success messages, and any UI content shown to the user. It returns the combined text of the element and all of its descendants, and it strips surrounding whitespace so your assertions stay clean.
Because getText() depends on visibility, it works best when the element is present and visible. On dynamic pages it is good practice to wait for the element before reading its text, which avoids intermittent empty results.
Locate the element with any locator such as ID, CSS selector, or XPath, then call getText() on the returned WebElement. The Java example below reads a heading and asserts on it.
import org.openqa.selenium.By;
import org.openqa.selenium.WebElement;
// Assuming 'driver' is an initialized WebDriver instance
WebElement heading = driver.findElement(By.cssSelector("#welcome-banner"));
String text = heading.getText();
System.out.println("Element text: " + text);
// Use it in an assertion
if (text.equals("Welcome back!")) {
System.out.println("Banner text is correct");
}If content loads asynchronously, calling getText() too early returns an empty string. Pair it with an explicit wait so Selenium reads the text only after the element is visible.
import org.openqa.selenium.By;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.ui.WebDriverWait;
import org.openqa.selenium.support.ui.ExpectedConditions;
import java.time.Duration;
WebDriverWait wait = new WebDriverWait(driver, Duration.ofSeconds(10));
WebElement message = wait.until(
ExpectedConditions.visibilityOfElementLocated(By.id("status-message"))
);
System.out.println("Status: " + message.getText());For more on wait strategies, see the types of waits available in WebDriver.
These two methods retrieve different things, and mixing them up is a common source of empty or wrong results:
To locate elements by their text in the first place, see the guide on how to find an element by text in Selenium.
In the Python bindings, text is exposed as a property named text rather than a method call, but the behavior matches getText().
from selenium import webdriver
from selenium.webdriver.common.by import By
driver = webdriver.Chrome()
driver.get("https://www.example.com")
element = driver.find_element(By.CSS_SELECTOR, "#some-element")
text = element.text
print(text)
driver.quit()The exact text a user sees can vary with locale, font rendering, and responsive layouts across browsers, so text assertions should be checked on more than one local environment. TestMu AI lets you run your Selenium suites on a cloud of 3000+ real browsers and operating systems, so a getText() assertion that passes locally is confirmed on the same browsers and devices your users have. Extending your automation testing and cross-browser testing this way catches localization and rendering differences a single-browser run would miss.
Getting text from a web element in Selenium comes down to choosing the right method: use getText() for visible, rendered text, and getAttribute() for input values, hidden text, or other attributes. Wait for the element to be visible before reading it, watch out for child-element text and stale references, and validate your assertions across real browsers. To build on this, learn how to check whether an element is displayed.
getText() returns the visible, rendered text between an element's tags. getAttribute() returns the value of a specific HTML attribute, such as value, href, or placeholder. For input fields, use getAttribute("value") because the typed text is stored in the value attribute, not as visible text.
getText() only returns text that is visually rendered. If the element is hidden by CSS such as display:none or visibility:hidden, or has not finished loading, it returns an empty string. Use an explicit wait for visibility, or read the value attribute for hidden content.
Text typed into an input or textarea lives in the value attribute, not as inner text, so getText() usually returns empty. Use element.getAttribute("value") to read exactly what the user entered into a form field.
getText() returns the combined text of an element and all its children. To get only the parent's own text, use a JavaScriptExecutor to read the first text node, or extract the child text separately and remove it from the full string.
No. getText() automatically trims leading and trailing whitespace and returns the visible text as rendered. If you need the raw or untrimmed content, use getAttribute("innerText") or getAttribute("textContent") instead.
In Python bindings, text is exposed as a property rather than a method. Locate the element and read element.text, which behaves like getText() in Java and returns the visible, trimmed text of the element and its children.
KaneAI - Testing Assistant
World’s first AI-Native E2E testing agent.

TestMu AI forEnterprise
Get access to solutions built on Enterprise
grade security, privacy, & compliance