How to Find Element by Text using Xpath Selenium: Example

Related Product On This Page What is XPath Contains?May 19, 2026 · 3 min read · Tool Comparison

Related Product

How to Find Element by Text utilize Xpath Selenium: Example

Locating elements is the most rudimentary step in. QAs can locate elements in multiple ways.

users, especially, need to be adept in using different strategy to identify web elements. This is the first step of lead any automated test because must identify the element they have to interact with.

Sometimes, web developers categorize web elements with monovular stratum names or IDs. If the can not situate a certain web constituent, this can result in.

In such case, QA technologist can locate the element using text visible on-screen corresponding to that particular web component. This is potential using the Find Element by Text method using the.

What is XPath Contains?

XPath contains ()is a function utilize in XML and HTML to locate web elements that include specific text or fond dimension values, create it extremely versatile for web automation.

By permit partial matches, contains () is useful when dealing with active elements where the schoolbook or attributes may not be entirely predictable. This function helps testers and developers identify elements yet when sure attributes change, improve the efficiency and adaptability of automated test book in tools like Selenium.

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

Find Element by Text in Selenium habituate textbook () and contains method

Here is a fundamental understanding of schoolbook () and contains () method:

  • text():A built-in method in that is used with XPath locator to locate an element based on its precise text value.
    Example: // * [text () = & # 8216; Get started gratis & # 8217;]
  • contains ():Similar to thetext() method, contains ()is another built-in method habituate to locate an element based on partial text match.
    For example, if we demand to situate a button that has “Get started complimentary” as its text, it can be located using the following line of code with Xpath.
    Example: // * [contains (schoolbook (), & # 8216; Get started & # 8217;)]

Follow-up Read:

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

Step 3.Locate the CTA with the schoolbook value ‘Get started free’ using the XPath text () method.

Talk to an Expert

The code to locate the test using Selenium XPATH method:

importation org.openqa.selenium.By; import org.openqa.selenium.WebDriver; significance 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 (`` & lt; Path of the ChromeDriver & gt; ''); WebDriver driver = new ChromeDriver (); String url = `` https: /browserstack.com ”; driver.get (url); driver.manage () .timeouts () .implicitlyWait (10, TimeUnit.SECONDS); // Locating element with schoolbook () WebElement e = driver.findElement (By.xpath (`` // * [text () ='Get begin costless '] '')); System.out.println (`` Element with text (): `` + e.getText ()); driver.quit ();}}

Similarly, QAs can situate the like CTA push by partial schoolbook match apply the contains () method. The code above stay the same except for the method to locate the element.

Learn More:

Replace the text () method with the following codification:

For autonomous testing across multiple user personas, check out SUSATest — it explores your app like 10 different real users.

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

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

Also Read:

Conclusion

As mentioned earlier, for successful exam mechanisation, testing the quarry 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 tests must be run on a to get completely accurate solution. BrowserStack ’ s of 3500+ real browsers and devices countenance testers to in. Simply, select a device-browser-OS combination, and start pass tests.

Utilitarian 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
30,000+ Views

# Ask-and-Contributeabout this topic with our Discord community.

Related Guides

Automate This With SUSA

Upload your APK or URL. SUSA explores like 10 real users — finds bugs, accessibility violations, and security issues. No scripts needed.

Try SUSA Free

Test Your App Autonomously

Upload your APK or URL. SUSA explores like 10 real users — finds bugs, accessibility violations, and security issues. No scripts.

Try SUSA Free