An In-Depth Look at Selenium Find Element by Text with Example:
Table of Contents:
Selenium Find Element That Contains Specific Text
Selenium Find element by text is used to locate a web element using its text value. The text value is generally used when the basic element identification properties such as ID or class have failed.
Sometimes, developers tend to group similar web elements with the same ID or the same class together. In such a case, find web element using text comes to the rescue of automation testing.
The text value can be fully matched or partially matched to locate the element. By the end of this tutorial, you will gain clear knowledge about Selenium find element.
Below is an Example of the use of text method to find a specific web element.
- Open the website – SoftwareTestingHelp.com
- Find the hyperlink – Manual Testing using the text property.
The above task can be accomplished using the inbuilt text method as mentioned below:
WebElement textDemo = driver.findElement(By.xpath(“//*”));
Text() Method of Selenium
- Text() method is a built-in method of selenium web driver that can be used to locate an element based on the text of the web element.
- Below is an example that demonstrates the usage of text method in Selenium.
Test Scenario
- Open Firefox browser with the URL: SoftwareTestingHelp.com
- Using text method of selenium web driver, find the web element with text – Write and Earn.
- Validate if the selected element is displayed on the web page.
- If it is displayed, print the text as Element found using text.
- If the element is not displayed, print the text as Element not found.
Source code:
package Demo; import java.util.List; import org.openqa.selenium.By; import org.openqa.selenium.WebDriver; import org.openqa.selenium.WebElement; import org.openqa.selenium.firefox.FirefoxDriver; public class FindElementDemo { public static void main(String[] args) throws InterruptedException { // TODO Auto-generated method stub System.setProperty("webdriver.gecko.driver", "D:\\Data_Personal\\Demo\\geckodriver-v0.23.0-win64\\geckodriver.exe"); WebDriver driver = new FirefoxDriver(); driver.get("https://www.softwaretestinghelp.com/"); WebElement textDemo = driver.findElement(By.xpath("//*[text()='Write and Earn']")); if(textDemo.isDisplayed()) { System.out.println("Element found using text"); } else System.out.println("Element not found"); driver.quit(); } }
Console Output:
Code Explanation:
- Initially, we are creating an instance of the Firefox browser using gecko driver.
- Using the driver.get() method, we are navigating to the URL: SoftwareTestingHelp
- Then, we are trying to find the element with the text – Write and Earn (Hyperlink).
- If the web element is displayed, we are adding a print statement saying element found using the specified text.
- If not, we are printing element not found message.
- Finally, we are closing the browser session using the driver.quit() method.
Suggested Read => In-Depth Free Selenium Training Tutorials
Contains Method of Selenium
- Contains method is used to find web elements with partial text match.
- For Example, if we want to find the list of web elements that contain the word ‘Selenium’, then we can do so using the built-in contains method as mentioned below.
List<WebElement> elementsList = driver.findElements(By.xpath(“//*[contains(text(),'Selenium')]"));
Example:
Test Scenario
- Open Firefox browser with the URL: SoftwareTestingHelp.com
- Using contains method, find the list of web elements which contain the text – Write and Earn.
- Print the count of the number of elements found in the list.
Source code:
package Demo; import java.util.List; import org.openqa.selenium.By; import org.openqa.selenium.WebDriver; import org.openqa.selenium.WebElement; import org.openqa.selenium.firefox.FirefoxDriver; public class FindElementDemo { public static void main(String[] args) throws InterruptedException { // TODO Auto-generated method stub System.setProperty("webdriver.gecko.driver", "D:\\Data_Personal\\Demo \\geckodriver-v0.23.0-win64\\geckodriver.exe"); WebDriver driver = new FirefoxDriver(); driver.get("https://www.softwaretestinghelp.com/"); List<WebElement>textDemo= driver.findElements(By.xpath("//*[contains(text(),'Write and Earn')]")); System.out.println("Number of web elements: " +textDemo.size()); driver.quit(); } }
Console Output:
Code Explanation:
- In the first step, we are initializing gecko driver instance to point to a geckodriver.exe file.
- Then, we are navigating to the URL https://www.softwaretestinghelp.com/
- Using contains method, we are trying to find the web elements with the text “Write and Earn”.
- Using the size method, we are counting the number of elements with the specified text and printing it on the console.
- Finally, we are closing the web browser session using the driver.quit() method.
Difference between Text, Link Text, and Partial Link Text Methods
- Text, link text, and partial link text methods are all the built-in methods provided by Selenium web driver.
- Text method is used to identify a web element uniquely using the property text.
- Link text is used to identify a web element uniquely using the property link text, with an exact match.
- Partial link text is used to identify a web element uniquely using the property link text, not necessarily the exact match.
- Link text and Partial link text are both case sensitive, which means upper case and lower case difference matters.
Example:
Test Scenario:
- Open the website SoftwareTestingHelp.com using the Firefox web browser.
- Find the web element – Write and Earn a link using the link text method.
- Find the web element – Write and Earn link using the partial link text method.
- Find the web element – Write and Earn link using the text method.
Below is the source code for the above test scenario.
Source code:
package Demo; import java.util.List; import org.openqa.selenium.By; import org.openqa.selenium.WebDriver; import org.openqa.selenium.WebElement; import org.openqa.selenium.firefox.FirefoxDriver; public final class LinkTextDemo { public static void main(String[] args) throws InterruptedException { // TODO Auto-generated method stub System.setProperty("webdriver.gecko.driver", "D:\\Data_Personal\\Demo\\geckodriver-v0.23.0-win64\\geckodriver.exe"); WebDriver driver = new FirefoxDriver(); driver.get("https://www.softwaretestinghelp.com/"); WebElement linkText = driver.findElement(By.linkText("Write and Earn")); if(linkText.isDisplayed()) { System.out.println("Element using link text is found"); } WebElement partialLinkText = driver.findElement(By.partialLinkText("Write")); if(partialLinkText.isDisplayed()) { System.out.println("Element using partial link text is found"); } List<WebElement> textDemo = driver.findElements(By.xpath("//*[contains(text(),'Write and Earn')]")); if(textDemo.isEmpty()) { System.out.println("Element using text is not found"); } else System.out.println("Element using text is found"); driver.quit(); } }
Code Output:
Code Explanation:
- In the first step, we are setting the system property i.e. webdriver.gecko.driver to point to the local location of the geckodriver.exe file.
- We are then initializing an instance of the firefox driver and navigating to the URL – https://www.SoftwareTestingHelp.com
- We are initially trying to identify the web element – Write and Earn using the link text and printing the element identification status onto the eclipse console.
- We are initially trying to identify the web element – Write and Earn using the partial link text and printing the element identification status onto the eclipse console.
- We are initially trying to identify the web element – Write and Earn using the text method and printing the element identification status onto the eclipse console.
Conclusion
- Find element by text is used to locate a web element using its text value. Predefined method text() is used to achieve the same.
- Contains method is used to find web elements with partial text match.
- Text method is used to identify a web element uniquely using the property text.
- Link text is used to identify a web element uniquely using the property link text, with an exact match.
- Partial link text is used to identify a web element uniquely using the property link text, not necessarily the exact match.