Home Guide How to Find Element by Text in Selenium: Tutorial

How to Find Element by Text in Selenium: Tutorial

By Jash Unadkat, Technical Content Writer at BrowserStack -

Locating elements is the most fundamental step in web test automation. There are multiple ways by which QAs can locate elements. Selenium users, especially, need to be proficient in using different locator strategies to identify web elements. This is the first step of running any automated test because test scripts must identify the element it has to interact with.

Refer to this detailed guide on Locators in Selenium to learn more about various locator strategies in Selenium.

In some cases, web developers tend to categorize web elements with identical class names or IDs. This can result in failed tests if the WebDriver cannot locate a certain web element.

In such cases, QA engineers can locate the element using text visible on-screen corresponding to that particular web element. This is possible using the Find Element by Text method using the Xpath locator.

Let’s consider an example depicting how to find an element by text in Selenium.

Prior to the example, let’s gather a fundamental understanding of the following two methods:

  1. text(): A built-in method in Selenium WebDriver that is used with XPath locator to locate an element based on its exact text value.
    Example: //*[ text() = ‘Get started free’ ]
  2. contains(): Similar to the text() method, contains() is another built-in method used to locate an element based on partial text match.
    For example, if we need to locate a button that has “Get started free” as its text, it can be located using the following line of code with Xpath.
    Example: //*[ contains (text(), ‘Get started’ ) ]

Let’s consider a simple scenario

  1. Launch the Chrome browser
  2. Navigate to BrowserStack’s website
  3. Locate the CTA with text value ‘Get started free’ using the XPath text() method.Selenium Find Element By Text

The code to locate the CTA using text() method is as follows:

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import java.util.concurrent.TimeUnit;
public class Match{
public static void main(String[] args) {

System.setProperty("<Path of the ChromeDriver>");

WebDriver driver = new ChromeDriver();
String url = "https:/browserstack.com”;
driver.get(url);
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);

// Locating element with text()
WebElement e = driver.findElement(By.xpath("//*[text()='Get started free']"));

System.out.println("Element with text(): " + e.getText() );
driver.quit();
}
}

Similarly, QAs can locate the same CTA button by partial text match using the contains() method. The code above remains the same except the method to locate the element.

Run Selenium Tests on Real Device Cloud for Free

Replace the text() method with the following code:

// located element with contains()
WebElement m = driver.findElement (By.xpath ("//*[contains(text(),'Get started ')]"));

The method above will locate the “Get started free” CTA based on the partial text match made against the string ‘Get started’.

As mentioned earlier, for successful test automation, testing the target web elements is of utmost importance. To do so, QA engineers must be able to use different locator strategies. The Selenium find element by text method can prove useful for QAs seeking to re-execute failed tests by locating elements accurately based on its text.

Bear in mind that Selenium tests must be run on a real device cloud to get completely accurate results. BrowserStack’s cloud Selenium grid of 2000+ real browsers and devices allows testers to automated visual UI tests in real user conditions. Simply sign up for free, select a device-browser-OS combination, and start running tests.

BrowserStack Logo Run Selenium Tests on 2000+ Browsers & Devices Run a Free Test Now