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.
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.
- vstMyPg.click();
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;
import org.openqa.selenium.chrome.ChromeDriver;
public class FindByID{
public static void main(String[] args) {
System.setProperty("webdriver.chrome.driver", "D:\\chromedriver_win32\\chromedriver.exe");
WebDriver driver=new ChromeDriver();
driver.navigate().to("http://only-testing-blog.blogspot.com/2013/09/testing.html");
//selenium find by link text.
WebElement vstMyPg= driver.findElement(By.linkText("Test Link"));
vstMyPg.click();
driver.close();
}
}
It will find link and click on it.
No comments:
Post a Comment