Testing With Selenium WebDriver (65 Blogs)
AWS Global Infrastructure

Software Testing

Topics Covered
  • Testing With Selenium WebDriver (55 Blogs)
SEE MORE

MI-new-launch

myMock Interview Service for Real Tech Jobs

myMock-widget-banner-bg

How to Use Link Text in Selenium?

Published on Jul 18,2019 186 Views
A tech enthusiast in Java, Image Processing, Cloud Computing, Hadoop. A tech enthusiast in Java, Image Processing, Cloud Computing, Hadoop.
4 / 8 Blog from Locators and Waits in Selenium

MI-new-launch

myMock Interview Service for Real Tech Jobs

myMock-mobile-banner-bg

myMock Interview Service for Real Tech Jobs

  • Mock interview in latest tech domains i.e JAVA, AI, DEVOPS,etc
  • Get interviewed by leading tech experts
  • Real time assement report and video recording

Locators in Selenium are used to uniquely identify the web element on the web-page. There are various locators like Id, name, CSS Selector, XPath that serve different purposes. In order to locate a particular button or a link on the web-page, we use link Text locator. In this article, you will see to locate an element using Link Text in Selenium.

Below topics are covered in this article:

Let’s get started!

Link Text in Selenium

A linkText is used to identify the hyperlinks on a web page.  It can be determined with the help of an anchor tag (<a>). In order to create the hyperlinks on a web page, you can use anchor tags followed by the linkText.

Now, let’s examine linkText locator with the help of an example. Suppose you want to locate the “Sign Up”  link as shown in the below snapshot. How will you do that?

Let me take you through the steps.

Twitter linkText locator - linkText in Selenium - Edureka

On inspecting “Sign Up  button – you can notice that it starts with an anchor tag in the below snippet. But, this anchor tag doesn’t have any name and Id attributes. In such cases, you can use linkText locator.

Inspecting a link - link text in Selenium - Edureka

In the above snippet, it consists of a text called “Sign Up”. I will make use of that text and use a linkText to write my code as shown below.

package Edureka;
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.By;
import org.openqa.selenium.chrome.ChromeDriver;
public class Locators {
public static void main(String[] args) {

//Configuring chrome driver
System.setProperty("webdriver.chrome.driver", "C:Selenium-java-edurekachromedriver_win32chromedriver.exe");
WebDriver driver = new ChromeDriver();

//maximizing the window and deleting cookies
driver.manage().window().maximize();
driver.manage().deleteAllCookies();
//Assigning page timeout and implicit wait
driver.manage().timeouts().pageLoadTimeout(40, TimeUnit.SECONDS);
driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
//navigating through the particular website
driver.get("https://twitter.com/");
driver.findElement(By.linkText("Sign Up")).click(); //linkText locator for links
}
}

When you run the above Java program, chrome driver  will launch Google Chrome, redirect to twitter home page and hit on the “Sign Up” button, and navigate to the next page. You can refer below snapshot for the output:

 

So, that’s how it works. Now let’s move further and see how to locate an element with the help of Partial Link Text.

Partial Link Text locator

In some situations, you may need to find links by a portion of the text in a linkText element. In such situations, you can use Partial Link Text to locate elements. Let’s take the same example and try to locate it. I will choose the “Sign Up”  link. Now, instead of pasting full text I will just give it as Sign. So, my code looks like:

driver.get("https://twitter.com/");
driver.findElement(By.partialLinkText("Sign")).click(); //partiallinkText locator for links

Now, when you run the above code, it will be redirected to “Sign Up  page as shown in the above output snapshot, but the difference is that you are using partial value to locate the links. I hope this gives you a clear understanding of how linkText and partialLinkText locator in selenium works.

Note:

Suppose there are multiple links with the same text value. Take a look at the below snapshot which has two buttons with the same name.

LinkText locator example - LinkText in Selenium - Edureka

Here, both the Log in links serve the same purpose. But, you want to locate the first Log in. How will you do that? In such cases, you cannot use linkText or partialLinkText, but you can make use of other locators like XPath or CSS Selectors. If you wish to know how to identify and locate web elements using XPath and CSS Selectors, you can check out these articles on XPath in Selenium and CSS Selectors.

With this, we come to an end of this article on Link Text in Selenium. I hope it helped you and added value to your knowledge.

If you wish to learn Selenium and build a career in the testing domain, then check out our interactive, live-online Selenium Certification Training here, that comes with 24*7 support to guide you throughout your learning period.

Got a question for us? Please mention it in the comments section of  “LinkText in Selenium” article and we will get back to you.

Comments
0 Comments

Browse Categories

Subscribe to our Newsletter, and get personalized recommendations.