How to Find Element by Text in Selenium

Vipul Gupta

Posted On: June 2, 2025

view count187966 Views

Read time11 Min Read

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.

What Is Find Element by Text in Selenium?

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.

Methods to Find Element by Text in Selenium

To find element by text in Selenium, you can use the text() and contains() methods in your XPath with the findElement() method.

  • text(): This built-in method in Selenium is used with XPath to locate an element based on its exact text value:

You can also extract text from a web page and print it in the console using the getText() method.

  • contains(): It is another built-in method which is used with XPath. However, this is used when writing the locator based on a partial text match.

Using findElement() Method for Complete Text Match

To find element by text in Selenium for complete text match, you can use the findElement() method. Here’s an example:

Test Scenario:

  1. Log in to the LambdaTest Selenium Playground.
  2. Identify the WebElement for the Checkbox Demo link using the text() method.
  3. Click on it and print the page header.

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


Step 1: Inspect the Element

    1. Open the web page and right-click on the Checkbox Demo link.
    2. Click Inspect to open the browser developer tools. In the Elements tab, you’ll see the HTML structure of the web page.
    3. Use the anchor tag (a) and the text “Checkbox Demo” to find this element using XPath.

    Step 2: Setup Project

    1. Open Eclipse IDE and create a Maven project named FindElementByText.
    2. Inside the src folder, add a new package named test to hold all test files.
    3. Create a BaseTest.java file to handle WebDriver setup, teardown, and LambdaTest cloud grid connection.

    Step 3: Implementation

    1. Update your pom.xml file with Selenium and TestNG dependencies.
    2. Create BaseTest.java file to set up and tear down the WebDriver, with connectivity to LambdaTest Selenium Grid.

      You can get your LambdaTest 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 LambdaTest platform to support test execution on their cloud grid.

      You can fetch the required browser capabilities for the LambdaTest platform by navigating to the Automation Capabilities Generator.

    3. Create TestFindByTextForCompleteMatch.java file to perform the actual text match operation.

    Once the tests are executed, you can view your test results on the LambdaTest Web Automation dashboard.

    lambdatest web automation dashboard

    Using findElement() Method for Partial Text Match

    To find element by text in Selenium for a partial text match, you can use XPath with the contains() method.

    Test Scenario:

    1. Log in to the LambdaTest Selenium Playground.
    2. Identify all the WebElements that have “Table” in their names.
    3. Print the text of all such WebElements.

    Step 1: Inspect the Element

      1. Right-click on any WebElement with “Table” in its name and select Inspect to open the browser developer tools.
      2. In the Elements tab, use the anchor tag and partial text “Table” with XPath contains() method to locate these elements.
      3. 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.

      Step 2: Implementation

      Create a new test class file named TestFindByTextForPartialMatch.java and add the following code.

      Once the test executes, you can verify the results in the LambdaTest Web Automation dashboard.

      Using findElement() Method With text() and contains() Methods

      Let’s look at how to find element by text in Selenium for finding a WebElement that contains a specific text using the text() and contains() methods.

      Test Scenario:

      1. Log in to the LambdaTest Selenium Playground.
      2. Identify WebElements that have “Dynamic” in its name and click on it.
      3. Print the header of the loaded page after the action.

      Step 1: Inspect the Element

      Right-click on the Dynamic Data Loading link and select Inspect to open developer tools and identify the element’s locator.

      Step 2: Implementation

      We target the element containing the specific text “Dynamic” to perform the click action. To achieve this, we use the findElement() method with an XPath expression that combines the text() and contains() methods.

      Create a new test class file called TestFindByTextForSpecificContainingText.java and add the below code to perform a set of actions.

      Once the tests are executed, you can view your test results on the LambdaTest Web Automation dashboard.

      Conclusion

      In this blog on how to find element by text in Selenium, we explored finding an element using text in Selenium. We saw how to use the text() method in complete and partial text matches. We also saw how we can use it in the case of the findElements() method and get a list of WebElements through text match.

      You can also check this blog on findElement() and findElements() in Selenium to know their key differences.

      Frequently Asked Questions (FAQs)

      How do you search for text in Selenium?

      The findElement() method returns a WebElement object. This object has a getText() method, which returns the visible text on that element. Use the findElement() method with appropriate locators to find a particular element.

      What is text() in XPath?

      In XPath, text() is a node test that matches a text node. A text node is the kind of node that contains the text between the start and end tags when elements are written in HTML source code or XML source code.

      How to find an element by text in XPath?

      You can use the text content of an element in your XPath expression to locate it. This is helpful when the element doesn’t have unique attributes, and you want to match it based on the exact words it shows on the web page.

      What is the purpose of getText() and getAttribute() in Selenium?

      The getText() method is used to read the visible text of a web element on the page, while the getAttribute() method lets you access the value of a specific attribute of that element, such as its link, placeholder, or any custom property.

      How do you find elements in Selenium?

      In Selenium, you can find elements using different types of locators such as ID, Name, TagName, ClassName, XPath, CSS Selector, etc. The best method to use depends on how the element is defined in the HTML.

      When should I use text-based XPath in Selenium?

      Text-based XPath is useful when other attributes like ID or class are missing, not unique, or keep changing. It helps you locate elements based on the exact visible text shown on the web page.

      Can I find an element using partial text in Selenium?

      Yes, you can find elements using a part of the text. This is helpful when the full text is too long or keeps changing slightly, but a certain part remains constant.

      What are the risks of relying only on text to find elements?

      Using only visible text to find elements can make tests fragile. If the text changes due to a content update or translation, your test might fail. It’s good to combine this method with other stable locators when possible.

      Citations

Author Profile Author Profile Author Profile

Author’s Profile

Vipul Gupta

Vipul Gupta is a passionate Quality Engineer with 6+ years of experience and keen interest in automation testing of Web and API based applications. He is having experience in designing and maintaining various automation frameworks. Currently working as Sr. SDET, he enjoys reading and learning about new test practices and frameworks.

Blogs: 23



linkedintwitter

Test Your Web Or Mobile Apps On 3000+ Browsers

Signup for free