How to Use Link Text and Partial Link Text in Selenium WebDriver? (2024)

Last updated on Sep 10,2024 48.8K Views

Share

WhatsAppLinkedinTwitterFacebookRedditHow to Use Link Text and Partial Link Text in Selenium WebDriver? (6)Copy Link!


How to Use Link Text and Partial Link Text in Selenium WebDriver? (7)

Neha Vaidya A tech enthusiast in Java, Image Processing, Cloud Computing, Hadoop. A tech enthusiast in Java, Image Processing, Cloud Computing, Hadoop.

  • Bookmark

4 / 8 Blog from Locators and Waits in Selenium

Become a Certified Professional

In this article

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.

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.

How to Use Link Text and Partial Link Text in Selenium WebDriver? (14)

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 linkTextlocator.

How to Use Link Text and Partial Link Text in Selenium WebDriver? (15)

To know more about Automation Testing, join our Automation Testing Course online. Also, If you wish to master the principles in Automation Testingand work on step-by-step tasks that have significant relevance for the world of business, industry professionals developed the Automation Testing syllabus.

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 driverSystem.setProperty("webdriver.chrome.driver", "C:Selenium-java-edurekachromedriver_win32chromedriver.exe");WebDriver driver = new ChromeDriver();//maximizing the window and deleting cookiesdriver.manage().window().maximize();driver.manage().deleteAllCookies();//Assigning page timeout and implicit waitdriver.manage().timeouts().pageLoadTimeout(40, TimeUnit.SECONDS);driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);//navigating through the particular websitedriver.get("https://twitter.com/");driver.findElement(By.linkText("Sign Up")).click(); //linkText locator for links}}

When you run the aboveJavaprogram,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:

How to Use Link Text and Partial Link Text in Selenium WebDriver? (16)

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.

You can even check out the details of Selenium with the Selenium Training in India.

Partial Link Text locator

In some situations, you may need to find links by a portion of the text in alinkTextelement. In such situations, you can usePartial Link Textto 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 asSign. 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.

How to Use Link Text and Partial Link Text in Selenium WebDriver? (17)

Here, both the Log in links servethe 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-onlineSelenium Certification, which comes with 24*7 support to guide you throughout your learning period.

Find out our Selenium Training in Top Cities/Countries

IndiaOther Cities/Countries
BangaloreUS
HyderabadUK
PuneCanada
ChennaiAustralia
MumbaiSingapore
KolkataEdinburgh

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

How to Use Link Text and Partial Link Text in Selenium WebDriver? (2024)
Top Articles
Buy | eBay Motors Explained
Who Owns Netflix? Here's Who Made & Created the Streaming Service
Libiyi Sawsharpener
Lorton Transfer Station
Wizard Build Season 28
How to change your Android phone's default Google account
Craigslist In Fredericksburg
Riegler &amp; Partner Holding GmbH auf LinkedIn: Wie schätzen Sie die Entwicklung der Wohnraumschaffung und Bauwirtschaft…
Xrarse
Heska Ulite
Giovanna Ewbank Nua
Garrick Joker'' Hastings Sentenced
R Tiktoksweets
R/Afkarena
My.tcctrack
Blackwolf Run Pro Shop
The Exorcist: Believer (2023) Showtimes
[Cheryll Glotfelty, Harold Fromm] The Ecocriticism(z-lib.org)
Why Should We Hire You? - Professional Answers for 2024
Sea To Dallas Google Flights
Why do rebates take so long to process?
Panola County Busted Newspaper
Sandals Travel Agent Login
The 15 Best Sites to Watch Movies for Free (Legally!)
Cars & Trucks - By Owner near Kissimmee, FL - craigslist
Gen 50 Kjv
Meijer Deli Trays Brochure
Truck from Finland, used truck for sale from Finland
Weather October 15
Hwy 57 Nursery Michie Tn
Log in or sign up to view
Darktide Terrifying Barrage
The value of R in SI units is _____?
South Florida residents must earn more than $100,000 to avoid being 'rent burdened'
Kattis-Solutions
UPS Drop Off Location Finder
Craigslist Ludington Michigan
Strange World Showtimes Near Atlas Cinemas Great Lakes Stadium 16
Giantess Feet Deviantart
4083519708
Nobodyhome.tv Reddit
Deshuesadero El Pulpo
Ursula Creed Datasheet
Pulitzer And Tony Winning Play About A Mathematical Genius Crossword
Sechrest Davis Funeral Home High Point Nc
Sacramentocraiglist
A Man Called Otto Showtimes Near Cinemark Greeley Mall
Erica Mena Net Worth Forbes
Craigslist Psl
Bomgas Cams
211475039
Latest Posts
Article information

Author: Neely Ledner

Last Updated:

Views: 5766

Rating: 4.1 / 5 (42 voted)

Reviews: 89% of readers found this page helpful

Author information

Name: Neely Ledner

Birthday: 1998-06-09

Address: 443 Barrows Terrace, New Jodyberg, CO 57462-5329

Phone: +2433516856029

Job: Central Legal Facilitator

Hobby: Backpacking, Jogging, Magic, Driving, Macrame, Embroidery, Foraging

Introduction: My name is Neely Ledner, I am a bright, determined, beautiful, adventurous, adventurous, spotless, calm person who loves writing and wants to share my knowledge and understanding with you.