testsigma
left-mobile-bg

How to Find Element by Text in Selenium: Tutorial with Example

October 24, 2024
right-mobile-bg
How to Find Element by Text in Selenium: Tutorial with Example
image

Start automating your tests 10X Faster in Simple English with Testsigma

Try for free

The most fundamental step in web test automation is element identification. There are numerous methods for QEs to locate elements. In Selenium, Find element by Text is used to locate a web element based on its text attribute. The text value is mostly used when the basic element identification properties, such as ID or Class, are dynamic and make it difficult to locate the web element.

In such cases,  it becomes very difficult to locate web elements. One option that comes to save us is text or partial text attributes.

In this blog, we will see how text or partial text can be useful when other IDs or class name are dynamic. 

Let’s get started. 

What is Find Element in Selenium?

When you begin writing your Selenium automation script, the interaction with web elements is the first and most important step because it is the WebElements you experiment with within your test script. Interaction with these web elements is now possible only if you identify them correctly.

“Find Element” in Selenium refers to the process of locating a web element on a page, such as a button, text box, or link, in order to interact with it through a script. This is typically done using the Selenium WebDriver’s find_element_by_locator method, where a locator can be a unique identifier of a web element such as “id”, “name”, “class name”, “xpath”, etc. The method returns the first web element that matches the search criteria.

The syntax for find element is:

WebElement element = driver.findElement(By.xpath(“//*[text()=’xyz’]”));

Different types of locators:

  • ID
  • Name
  • Class Name
  • Tag Name
  • Link Text
  • Partial Link Text
  • XPath
  • CSS Selector

What is findElement by Text in Selenium?

When you begin writing Selenium automation scripts, interaction with web elements is the first and most important step because these are the elements you experiment with within your test script. You can only interact with them when you identify them correctly.

“Find Element” refers to the process of locating a web element in Selenium on a page, such as a button, text box, or link, in order to interact with it through a script. This is typically done using the Selenium WebDriver’s find_element_by_locator method, where a locator can be a unique identifier of a web element such as “id”, “name”, “class name”, “xpath”, etc. The method returns the first web element that matches the search criteria.

One common use case is finding an element by its text content, which uses xpath in Selenium or link text (for hyperlinks). This method is particularly useful when the element doesn’t have a unique ID or class.

The syntax for findElement using xpath is:

WebElement element = driver.findElement(By.xpath(“//*[text()=’your_text’]”));

Example with xpath:

WebElement submitButton = driver.findElement(By.xpath(“//*[text()=’Submit’]”));

submitButton.click();

The syntax for findElement using link text is:

WebElement element = driver.findElement(By.linkText(“Exact Link Text”));

Example with link text:

WebElement homeLink = driver.findElement(By.linkText(“Home”));

homeLink.click();

The syntax for findElement using partial link text is:

WebElement element = driver.findElement(By.partialLinkText(“Partial Link Text”));

Example with particle link text:

WebElement homeLink = driver.findElement(By.partialLinkText(“Ho”));

homeLink.click();

Difference between findElement and findElements

findElement and findElements in Selenium with Java are used to locate web elements on a page.

The difference between the two methods is that findElement returns a single WebElement object that represents the first element on the page that matches the search criteria, while findElements returns a list of all WebElement objects that match the search criteria.

For example, if you want to locate a button with the text “Submit” on a page, you could use the following code:

WebElement submitButton = driver.findElement(By.xpath(“//button[text()=’Submit’]”));

And if you want to locate all the links on a page, you could use the following code:

List links = driver.findElements(By.tagName (“a”));

How to Find Element by Text in Selenium for a Complete Text match

To find an element by its text in Selenium for a complete text match in Java, you can use the XPath locator strategy and the text() function.

To understand it better, let’s consider a use case. 

Use case:

  1. Launch the Testsigma website.

  2. Identify the web element and capture the text of the button ‘Schedule a demo’

  3. Compare the expected value to the actual.

  4. Close the browser.

Code snippet: 


Using findElement() by Text with text() and contains() Methods

In Selenium, text() and contains() methods are used to identify and locate web elements based on the text they contain. 

The text() function matches the exact text content of an element. The above section shows an example of this.

The contains() method matches partial text content within an element.

An example of this would be locating an element whose text contains ‘Sub’ and clicking on it:

WebElement submitButton = driver.findElement(By.xpath(“//*[contains(text(), ‘Sub’)]”));

submitButton.click();


How to Find Elements by Text in Selenium for Partial Text match

The contains() function is a built-in XPath function that is used to search for a substring within a string.

To understand it better, let’s consider a use case.

Use case:

  1. Launch the website  – https://testsigma.com

  2. Identify the WebElement and capture the text of ‘’Build stable and reliable automated tests’

  3. Compare the expected value to the actual.

  4. Close the browser.

Code Snippet:


Conclusion

In this blog, we looked at finding an element using text() method. We saw how to use the text() method for both full and partial text matches. We also saw how we could use it in the FindElements case to get a list of Web Elements via text match. Using a text() can be really useful when other attributes such as IDs or class names are dynamic in nature, and they keep changing the moment you refresh your page.

As evident, to use Selenium, one needs to be an expert in one of the programming languages. There is where a no-code test automation comes in.

Explore Testsigma, a low code test automation tool, to automate your end-to-end UI tests for web, mobile, desktop and APIs, from the same place, 10x faster.

Start here

Happy Testing..!

Frequently Asked Questions

How do you search text in Selenium?

You can search for text in Selenium using the findElement or findElements method. The getText() method in Selenium Java is used to retrieve the text content of an element on a web page.

How do I find an element by tag?

In Selenium, you can find an element on a web page by its HTML tag name using the findElement or findElements method and the By.tagName locator strategy. Here’s an example using the findElement method to find a single input element:

WebElement element = driver.findElement(By.tagName(“input”));

How to use Selenium to find elements by Attribute Value?

In Selenium, you can find elements on a web page by their attribute value using the findElement or findElements method and a locator strategy such as By.xpath or By.cssSelector. Here’s an example using the findElement method to find an element with a specific value for the “value” attribute:

WebElement element = driver.findElement(By.xpath(“//*[@value=’Search Value’]”));

In this example, the driver is an instance of the WebDriver interface, and By.xpath(“//*[@value=’Search Value’]”) specifies the locator strategy and the value to search for.

You can replace “value” with any other attribute name, and “Search Value” with the desired attribute value, to search for elements with different attribute values.

Written By

Shreshtha Gokhru

Testsigma Author - Shreshtha Gokhru

Shreshtha Gokhru

An astute professional with over 8+ years of experience in Software testing, Agile methodologies, and product sales. I am also keen to invest in the Stock market and Mutual funds. Apart from that, I like travelling and playing cricket.

“Testsigma has been an absolute game-changer for us. We’ve saved time and caught critical issues that would’ve been missed with manual testing.“

- Bharathi K

Reach up to 70% test coverage with GenAI-based, low-code test automation tool.
User-friendly interface. Robust features. Always available support.

Testsigma - Momentum leader
Try for Free
imageimage
Subscribe to get all our latest blogs, updates delivered directly to your inbox.

By submitting the form, you would be accepting the Privacy Policy.

RELATED BLOGS


How to write Test cases for Dropdown?
AAYUSH SAXENA
TEST AUTOMATION
How to Write Test Cases for Trading Application Testing?
RITIKA KUMARI
TEST AUTOMATIONTESTING DISCUSSIONS
Scriptless Test Automation | What , Why it Matters & Examples
KIRUTHIKA DEVARAJ
TEST AUTOMATION