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

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

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

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: 


package demo; import static org.testng.Assert.assertEquals; import java.util.concurrent.TimeUnit; import org.openqa.selenium.By; import org.openqa.selenium.WebDriver; import org.openqa.selenium.WebElement; import org.openqa.selenium.chrome.ChromeDriver; public class CompleteText{ public static void main(String[] args) { System.setProperty(“webdriver.chrome.driver”, “C:\\Users\\sgokhru\\Downloads\\chromedriver_win32\\chromedriver.exe”); WebDriver driver = new ChromeDriver(); String url = “https://testsigma.com”; driver.get(url); driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS); // Locating element with text() WebElement element = driver.findElement(By.xpath(“//span[text()=’Schedule a demo ‘]”)); assertEquals(element .getText(), “Schedule a demo”); System.out.println(“Both the text are equal”); driver.quit(); } }

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:


package demo; 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 PartialText{ public static void main(String[] args) { System.setProperty(“webdriver.chrome.driver”, “C:\\Users\\sgokhru\\Downloads\\chromedriver_win32\\chromedriver.exe”); WebDriver driver = new ChromeDriver(); String url = “https://testsigma.com”; driver.get(url); //It will maximize the browser driver.manage().window().maximize(); driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS); // Locating element with text() WebElement element = driver.findElement(By.xpath(“//p[contains(text(), ‘Build stable and reliable automated tests’)]”)); System.out.println(“Element with text(): ” + element.getText()); driver.quit(); } }

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. Testsigma is a no-code, AI-powered test automation tool that lets you create test cases in minutes, using plain English NLPs. You don’t need to learn coding, and it’s open source too!

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.


Test automation made easy

Start your smart continuous testing journey today with Testsigma.

SHARE THIS BLOG

RELATED POSTS


Power of POC in Testing: Your Exclusive Guide to Success
Power of POC in Testing: Your Exclusive Guide to Success
performance testing tools_banner image
Test objects in software testing | Types & How to Create it?
How To Write Calculator Test Cases With Sample Test Cases
How To Write Calculator Test Cases? With Sample Test Cases