App & Browser Testing Made Easy

Give your users a seamless experience by testing on 3000+ real devices and browsers. Don't compromise with emulators and simulators

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.

Pro Tip: Want to dive deeper into Selenium implementation on BrowserStack with free interactive courses and lab exercises? Visit Test University

Run Selenium Tests on Real Device Cloud for Free

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

Find Element by Text in Selenium using text() and contains methods

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’ ) ]
What is the method needed to locate an element based on partial text match?

How to Find Element by Text in Selenium: Example

Let’s consider a simple scenario

  1. Launch the Chrome browser
  2. Navigate to BrowserStack’s website
  3. Locate the CTA with the 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 for 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 3000+ 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.

Tags
Automation Testing Selenium Selenium Webdriver

Featured Articles

findElement and findElements in Selenium

CSS Selector in Selenium: Locate Elements with Examples

Curated for all your Testing Needs

Actionable Insights, Tips, & Tutorials delivered in your Inbox
By subscribing , you agree to our Privacy Policy.
thank you illustration

Thank you for Subscribing!

Expect a curated list of guides shortly.