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

Learn how to findElement() by text in Selenium for dynamic IDs and Classes. Overcome challenges, enhance automation testing, and gain practical insights.
Vipul Gupta
January 13, 2026
Locating WebElements precisely is essential for reliable Selenium testing. One commonly used technique is to find elements by text in Selenium, which helps identify elements based on their visible text, especially when other attributes aren’t unique or consistent.
Why Use Selenium to Find Element by Text?
Locating elements by text in Selenium is essential for automating interactions with web elements, particularly when IDs or classes are dynamic. It enables precise targeting of elements based on their visible text content, ensuring that your test scripts can handle unpredictable changes in element identifiers.
Methods to Find Element by Text in Selenium
Selenium provides various methods to locate elements by text using XPath. The text() and contains() methods are most commonly used to perform exact or partial matches for visible text in elements:
Using findElement() Method for Complete Text Match
The findElement() method can be used to locate an element with a complete match to its text. This is ideal when you need to interact with elements that have static and predictable visible text:
Using findElement() Method for Partial Text Match
When you need to locate elements with partial text, the contains() method helps in selecting WebElements that contain a specific text string. This is especially useful when element text is dynamic or partially predictable:
How to Use XPath for Text-Based Element Selection
XPath is a powerful locator strategy in Selenium, and it allows you to find elements based on their visible text. It can be used to locate both exact and partial matches for the text in a WebElement, providing flexibility in automated tests:
Selenium Test Execution in TestMu AI
To run Selenium tests on dynamic elements, you can leverage the TestMu AI platform. It provides cloud-based Selenium grid capabilities, allowing you to execute tests on different browser and OS combinations without managing infrastructure. This simplifies parallel testing and reduces the need for local setup:
Find element by text in Selenium is a way to locate a WebElement based on its visible text content using the findElement() method. It is useful when attributes like ID or ClassName are dynamic or unreliable for identification.
XPath is typically used as the locator strategy, leveraging the text() method to find element by text in Selenium.
driver.findElement(By.xpath("//*[text()='Submit']"));
To find element by text in Selenium, you can use the text() and contains() methods in your XPath with the findElement() method.
WebElement ele = driver.findElement(By.xpath(“//[text()=’textvalue’]”))
You can also extract text from a web page and print it in the console using the getText() method.
WebElement ele =driver.findElement(By.xpath(“//[contains(text(),’textvalue’)]”))
To find element by text in Selenium for complete text match, you can use the findElement() method. Here’s an example:
Test Scenario:
Typically, you might run Selenium scripts on a local grid setup. However, here we’ll execute the test script on an online Selenium Grid using TestMu AI. This allows us to run tests in parallel across various browser and OS combinations without managing any infrastructure.
To get started, check out this documentation on Selenium testing with TestMu AI.
Note: Run Selenium tests across 3000+ real environments. Try TestMu AI Now!
Step 1: Inspect the Element
WebElement checkbox = driver.findElement(By.xpath("//a[text()='Checkbox Demo']"));
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>FindElementByText</groupId>
<artifactId>FindElementByText</artifactId>
<version>0.0.1-SNAPSHOT</version>
<build>
<sourceDirectory>src</sourceDirectory>
<plugins>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.1</version>
<configuration>
<release>17</release>
</configuration>
</plugin>
</plugins>
</build>
<dependencies>
<dependency>
<groupId>org.seleniumhq.selenium</groupId>
<artifactId>selenium-java</artifactId>
<version>4.16.1</version>
</dependency>
<dependency>
<groupId>org.testng</groupId>
<artifactId>testng</artifactId>
<version>7.8.0</version>
<scope>test</scope>
</dependency>
</dependencies>
</project>
public class BaseTest {
public RemoteWebDriver driver = null;
String username = System.getenv("LT_USERNAME") == null ? "<lambdatest_username>" : System.getenv("LT_USERNAME");
String accessKey = System.getenv("LT_ACCESS_KEY") == null ? "<lambdatest_accesskey>" : System.getenv("LT_ACCESS");
@BeforeTest
public void setup()
{
try
{
ChromeOptions chromeOptions = new ChromeOptions();
chromeOptions.setPlatformName("Windows 10");
chromeOptions.setBrowserVersion("121.0");
HashMap<String, Object> ltOptions = new HashMap<String, Object>();
ltOptions.put("build", "Find Element by Text");
ltOptions.put("project", "Find Element by Text in Selenium");
chromeOptions.setCapability("LT:Options", ltOptions);
driver = new RemoteWebDriver(
new URL("https://" + username + ":" + accessKey + "@hub.lambdatest.com/wd/hub"), chromeOptions);
}
catch (MalformedURLException e)
{
e.printStackTrace();
}
}
@AfterTest
public void tearDown()
{
driver.quit();
}
}
You can get your TestMu AI Username and Access Key from your Account Settings > Password & Security to connect to the cloud grid for test execution. Alternatively, you can configure these as environment variables and directly fetch them in the test script.
After that, create a HashMap type variable to pass the additional browser capabilities required by the TestMu AI platform to support test execution on their cloud grid.
You can fetch the required browser capabilities for the TestMu AI platform by navigating to the Automation Capabilities Generator.
public class TestFindByTextForCompleteMatch extends BaseTest{
@Test
public void testFindElementByCompleteTextMatch()
{
System.out.println("Logging into Lambda Test Selenium Playground");
driver.get("https://www.lambdatest.com/selenium-playground/");
WebElement checkBoxDemoPage= driver.findElement(By.xpath("//a[text()='Checkbox Demo']"));
checkBoxDemoPage.click();
System.out.println("Clicked on the Checkbox Demo Page");
WebElement header=driver.findElement(By.xpath("//h1"));
System.out.println("The header of the page is:"+header.getText());
}
}
If there are more than one WebElement, you can use the findElements() method.
The findElements() method returns the list of WebElements that match the locator value, unlike the findElement() method, which returns only a single WebElement.
If there are no matching elements within the web page, the findElements() method returns an empty list.
List tableOptions=driver.findElements(By.xpath(“//a[contains(text(),’Table’)”)
public class TestFindByTextForPartialMatch extends BaseTest {
@Test
public void testFindElementByPartialTextMatch() {
System.out.println("Logging into Lambda Test Selenium Playground");
driver.get("https://www.lambdatest.com/selenium-playground/");
List tableOptions = driver.findElements(By.xpath("//a[contains(text(),'Table')]"));
for (WebElement e : tableOptions) {
System.out.println("The different options with table in name are:" + e.getText());
}
}
}
WebElement element= driver.findElement(By.xpath(“//a[contains(text(),’Dynamic’)”)
public class TestFindByTextForSpecificContainingText extends BaseTest {
@Test
public void testFindElementBySpecificTextMatch() {
System.out.println("Logging into Lambda Test Selenium Playground");
driver.get("https://www.lambdatest.com/selenium-playground/");
WebElement element = driver.findElement(By.xpath("//a[contains(text(),'Dynamic')]"));
element.click();
System.out.println("Clicked on the Dynamic Data Loading link");
WebElement header = driver.findElement(By.xpath("//h1"));
System.out.println("The header of the page is:" + header.getText());
}
}
Did you find this page helpful?
More Related Hubs
TestMu AI forEnterprise
Get access to solutions built on Enterprise
grade security, privacy, & compliance