How to Find Element by Text in Selenium WebDriver
Vipul Gupta
Posted On: January 18, 2024
129753 Views
12 Min Read
Locating WebElements precisely is essential for reliable Selenium testing. One commonly used technique is to find element by text in Selenium, which helps identify elements based on their visible text, especially when other attributes aren’t unique or consistent.
TABLE OF CONTENTS
What Is Find Element by Text in Selenium?
In Selenium, findElement() method by text refers to locating a WebElement based on its visible text content. It is useful when attributes like ID or ClassName are dynamic or unreliable for identification.
To locate an element by text, XPath is typically used as the locator strategy, leveraging the text() function.
Methods to Find Element by Text in Selenium
To find element by text in Selenium, you can use 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:
- 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 webpage 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 match complete text using text() method, you can use findElement() method in Selenium. Here’s an example:
Test Scenario:
|
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.
Run Selenium tests across 3000+ real environments. Try LambdaTest Now!
Step 1: Inspect the Element
-
- Open the web page and right-click on the Checkbox Demo link.
- Click Inspect to open the browser developer tools. In the Elements tab, you’ll see the HTML structure of the web page.
- Use the anchor tag and the text “Checkbox Demo” to find this element using XPath.
Step 2: Setup Project
- Open Eclipse IDE and create a Maven project named FindElementByText.
- Inside the src folder, add a new package named test to hold all test files.
- Create a BaseTest.java file to handle WebDriver setup, teardown, and LambdaTest cloud grid connection.
- Update the pom.xml file with the latest stable versions of Selenium 4 and TestNG from the Maven repository.
Step 3: Implementation
- Update your pom.xml file with Selenium and TestNG dependencies:
- 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.
- Create TestFindByTextForCompleteMatch.java file to perform the actual text match operation.
Once the tests are completed and executed, you can view your test results on the LambdaTest Web Automation dashboard.
The video below will help you write your first test script in Selenium using Java by interacting with different WebElements.
Using findElement() Method for Partial Text Match
To locate an element with a partial text match in Selenium, you can use XPath with the contains() function to find elements containing the specified partial text.
Test Scenario:
|
Step 1: Inspect the Element
-
- Right-click on any WebElement with “Table” in its name and select Inspect to open the browser developer tools.
- In the Elements tab, use the anchor tag and partial text “Table” with XPath contains() function to locate these elements.
If there are more than one WebElement, we will use the findElements() method by text in Selenium.
The findElements() method by text in Selenium 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 text() and contains() methods.
Test Scenario:
|
Step 1: Inspect the Element
Right-click on the Dynamic Data Loading link and select Inspect to open DevTools 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 test code to perform a set of actions as shown below.
Once the tests are completed, you can also view your test results, logs, and the test recording as well in your 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 findElements() method and get a list of WebElements through text match.
Frequently Asked Questions (FAQs)
How do you search 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 do I find an element in Selenium?
You can use the following methods:
- ID
- Name
- ClassName
- TagName
- Link Text/Partial Link Text
- CSS Selector
- XPath
How to findElements() by text in Selenium?
To findElements() by text in Selenium, use the findElements() method provided by the Selenium WebDriver. This method will return a list of all the elements that match the given selector. The required element can then be accessed from the list using its index.
The most common approach to finding text elements is using XPath selectors with a text-based condition.
How to select WebElement by text in Selenium?
To select a WebElement by text in Selenium WebDriver, you can use the findElement() method with any locator. The best locator strategy will depend on the framework and use case.
-
- Using XPath, when you have plain text for locating the element.
-
- Using link text, when the element has a hyperlink and the exact link text is available and static.
-
- In cases where the exact link text is not available or is dynamic, Partial Link Text can be used to select the element using only the partial static part from it.
How to findElement() by text in XPath?
To findElement() by text in XPath, we can use the findElement() method with the XPath locator for an exact match.
-
- In case, the exact WebElement text is not available, use the contains() function to check for partial matches to locate the WebElement.
How to find elements by title text in Selenium?
To find an element by its title attribute text in Selenium, we can use either XPath or CSS Selectors with findElement() method.
-
- Using XPath:
-
- Using CSS:
Got Questions? Drop them on LambdaTest Community. Visit now