Chapter 6_3 : LinkText and PartialLinkText

Link Text :

 In this chapter, we will talk about “How To Locate Element By Link Text And Partial Link Text Locators”.

Lets take an example of  mercurytours.com

 We will click on “Register” link from below elements.

This Link Text locator works only on links (hyperlinks) that’s why it is called as Link Text locator.

When you try to run the Webdriver code , you will be accessing the “REGISTER” link.

When you execute webdriver code you will be taken over to Register link

Complete Code:

package testology;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
public class linkText
   {
public static void main(String[] args)
      {
System.setProperty("webdriver.driver.chromedriver", "chromedriver.exe");
		WebDriver driver = new ChromeDriver();
		driver.manage().window().maximize();
		driver.get("http://newtours.demoaut.com/");
WebElement register=driver.findElement(By.linkText("REGISTER"));
		register.click();
	}
}

Partial Link Text:

In some cases, we may need to find links by a portion of the text in a Link Text element it contains. In such cases, we use Partial Link Text to locate elements.below is the given html code.

<html>
<head>
<Title> Partial Link Text </Title>
</head>
	<body>
		<a href="http://www.facebook.com"> Click Here </a>
		<br>
		<a href="http://www.google.com"> Go </a>
	</body>
</html>

When you execute webdriver code you will be still taken over to Click Here link .

Complete Code:

package testology;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
public class partiallinktext
   {
public static void main(String[] args)
    {
System.setProperty("webdriver.driver.chromedriver", "chromedriver.exe");
		WebDriver driver = new ChromeDriver();
		driver.manage().window().maximize();
driver.get("file:///C:/Users/admin/Desktop/partial%20match.html");
WebElement partial=driver.findElement(By.partialLinkText("Click"));
partial.click();
System.out.println("Title of page is: " + driver.getTitle());							
   driver.quit();
      }
}