Test Elements in Selenium on Real Devices

Use Selenium to find Elements using Text and perform actions on it to test its functionality

Get Started free
Home Guide How to Find Element by Text using Xpath Selenium: Example

How to Find Element by Text using Xpath Selenium: Example

By Jash Unadkat, Community Contributor -

Locating elements is the most fundamental step in web test automation. QAs can locate elements in multiple ways.

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 they have to interact with.

Sometimes, web developers categorize web elements with identical class names or IDs. If the WebDriver cannot locate a certain web element, this can result in failed tests.

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.

What is XPath Contains?

XPath contains() is a function used in XML and HTML to locate web elements that include specific text or partial attribute values, making it highly versatile for web automation.

By allowing partial matches, contains() is useful when dealing with dynamic elements where the text or attributes may not be entirely predictable. This function helps testers and developers identify elements even when certain attributes change, improving the efficiency and adaptability of automated test scripts in tools like Selenium.

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

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

Here is a fundamental understanding of text() and contains() methods:

  • 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’ ]
  • 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’ ) ]

BrowserStack Automate Banner 7

How to Find Element by Text in Selenium: Example

Here is a demonstration on locating the CTA using the text() method with Selenium Xpath.

Step 1. Launch the Chrome browser

Step 2. Navigate to BrowserStack’s website

Step 3. Locate the CTA with the text value ‘Get started free’ using the XPath text() method.Selenium Find Element By Text

Talk to an Expert

The code to locate the test using Selenium XPATH method:

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.

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’.

Conclusion

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 3500+ 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.

Run Selenium Tests on Real Device Cloud for Free

Useful Resources for Selenium

Methods, Classes, and Commands

Configuration

XPath

Locators and Selectors

Waits in Selenium

Frameworks in Selenium

Miscellaneous

Best Practices, Tips and Tricks

Design Patterns in Selenium: Page Object Model and Page Factory

Action Class

TestNG and Selenium

JUnit and Selenium

Use Cases

Types of Testing with Selenium

Tags
Automation Testing Selenium Selenium Webdriver

Featured Articles

findElement and findElements in Selenium

CSS Selector in Selenium: Locate Elements with Examples

Automation Tests on Real Devices & Browsers

Seamlessly Run Automation Tests on 3500+ real Devices & Browsers