Find element by link text in selenium

Selenium find element by link text

Find element by link text is used only to locate hyperlinks on page. You can use By.linkText method of selenium to find link on page with exact match of text. Selenium find link by text value of hyperlink element.
  • Expression to find by link text : findElement(By.linkText("Text of link"));

How to find link text of hyperlink

Let us see how to get link text of hyper link to use it to locate and click on link.

selenium find element by link text

You can inspect link element as shown in above image to get text of link. That link can be located using below given syntax in selenium.

  • WebElement vstMyPg= driver.findElement(By.linkText("Test Link"));
Then you can click on link using below given syntax.
It will click on link find by linktext method of selenium.

Example of find by link text in selenium

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;

public class FindByID{

	public static void main(String[] args) {
		System.setProperty("", "D:\\chromedriver_win32\\chromedriver.exe");  
	    WebDriver driver=new ChromeDriver();  
	    //selenium find by link text.
	    WebElement vstMyPg= driver.findElement(By.linkText("Test Link"));;

It will find link and click on it.

No comments:

Post a Comment