
- Selenium - Home
- Selenium - Overview
- Selenium - Components
- Selenium - Automation Testing
- Selenium - Environment Setup
- Selenium - Remote Control
- Selenium - IDE Introduction
- Selenium - Features
- Selenium - Limitations
- Selenium - Installation
- Selenium - Creating Tests
- Selenium - Creating Script
- Selenium - Control Flow
- Selenium - Store Variables
- Selenium - Alerts & Popups
- Selenium - Selenese Commands
- Selenium - Actions Commands
- Selenium - Accessors Commands
- Selenium - Assertions Commands
- Selenium - Assert/Verify Methods
- Selenium - Locating Strategies
- Selenium - Script Debugging
- Selenium - Verification Points
- Selenium - Pattern Matching
- Selenium - JSON Data File
- Selenium - Browser Execution
- Selenium - User Extensions
- Selenium - Code Export
- Selenium - Emitting Code
- Selenium - JavaScript Functions
- Selenium - Plugins
- Selenium WebDriver Tutorial
- Selenium - Introduction
- Selenium WebDriver vs RC
- Selenium - Installation
- Selenium - First Test Script
- Selenium - Driver Sessions
- Selenium - Browser Options
- Selenium - Chrome Options
- Selenium - Edge Options
- Selenium - Firefox Options
- Selenium - Safari Options
- Selenium - Double Click
- Selenium - Right Click
- HTML Report in Python
- Handling Edit Boxes
- Selenium - Single Elements
- Selenium - Multiple Elements
- Selenium Web Elements
- Selenium - File Upload
- Selenium - Locator Strategies
- Selenium - Relative Locators
- Selenium - Finders
- Selenium - Find All Links
- Selenium - User Interactions
- Selenium - WebElement Commands
- Selenium - Browser Interactions
- Selenium - Browser Commands
- Selenium - Browser Navigation
- Selenium - Alerts & Popups
- Selenium - Handling Forms
- Selenium - Windows and Tabs
- Selenium - Handling Links
- Selenium - Input Boxes
- Selenium - Radio Button
- Selenium - Checkboxes
- Selenium - Dropdown Box
- Selenium - Handling IFrames
- Selenium - Handling Cookies
- Selenium - Date Time Picker
- Selenium - Dynamic Web Tables
- Selenium - Actions Class
- Selenium - Action Class
- Selenium - Keyboard Events
- Selenium - Key Up/Down
- Selenium - Copy and Paste
- Selenium - Handle Special Keys
- Selenium - Mouse Events
- Selenium - Drag and Drop
- Selenium - Pen Events
- Selenium - Scroll Operations
- Selenium - Waiting Strategies
- Selenium - Explicit/Implicit Wait
- Selenium - Support Features
- Selenium - Multi Select
- Selenium - Wait Support
- Selenium - Select Support
- Selenium - Color Support
- Selenium - ThreadGuard
- Selenium - Errors & Logging
- Selenium - Exception Handling
- Selenium - Miscellaneous
- Selenium - Handling Ajax Calls
- Selenium - JSON Data File
- Selenium - CSV Data File
- Selenium - Excel Data File
- Selenium - Cross Browser Testing
- Selenium - Multi Browser Testing
- Selenium - Multi Windows Testing
- Selenium - JavaScript Executor
- Selenium - Headless Execution
- Selenium - Capture Screenshots
- Selenium - Capture Videos
- Selenium - Page Object Model
- Selenium - Page Factory
- Selenium - Record & Playback
- Selenium - Frameworks
- Selenium - Browsing Context
- Selenium - DevTools
- Selenium Grid Tutorial
- Selenium - Overview
- Selenium - Architecture
- Selenium - Components
- Selenium - Configuration
- Selenium - Create Test Script
- Selenium - Test Execution
- Selenium - Endpoints
- Selenium - Customizing a Node
- Selenium Reporting Tools
- Selenium - Reporting Tools
- Selenium - TestNG
- Selenium - JUnit
- Selenium - Allure
- Selenium & other Technologies
- Selenium - Java Tutorial
- Selenium - Python Tutorial
- Selenium - C# Tutorial
- Selenium - Javascript Tutorial
- Selenium - Kotlin Tutorial
- Selenium - Ruby Tutorial
- Selenium - Maven & Jenkins
- Selenium - Database Testing
- Selenium - LogExpert Logging
- Selenium - Log4j Logging
- Selenium - Robot Framework
- Selenium - AutoIT
- Selenium - Flash Testing
- Selenium - Apache Ant
- Selenium - Github Tutorial
- Selenium - SoapUI
- Selenium - Cucumber
- Selenium - IntelliJ
- Selenium - XPath
- Selenium Miscellaneous Concepts
- Selenium - IE Driver
- Selenium - Automation Frameworks
- Selenium - Keyword Driven Framework
- Selenium - Data Driven Framework
- Selenium - Hybrid Driven Framework
- Selenium - SSL Certificate Error
- Selenium - Alternatives
Selenium WebDriver - Handling Links
Selenium Webdriver can be used to handle links on a web page. In HTML terminology, every link (referred to as hyperlinks) are identified by the tagname called anchor. Also, each link on a webpage has an attribute called href.
Identify of Links in HTML
Let us now discuss the identification of anchor tags for hyperlink - Created on a webpage shown in the below image. Right click on the webpage, and then click on the Inspect button in the Chrome browser. After that, the corresponding HTML code for the whole page would be visible. For investigating the Created link on a page, click on the left upward arrow as highlighted below.

Once, we had clicked and pointed the arrow to the Created hyperlink, its HTML code was visible.

A link can be identified using the link text locator in Selenium. The first element with the matching value of the link text is identified.
Syntax
Webdriver driver = new ChromeDriver(); driver.findElement(By.linkText("value of link text"));
Handle Links with Link Text Locator
Let us take an example of the above page, where on clicking the Created link, the text Link has responded with status 201 and status text Created would be visible on the page.

Example
package org.example; import org.openqa.selenium.By; import org.openqa.selenium.WebDriver; import org.openqa.selenium.WebElement; import org.openqa.selenium.chrome.ChromeDriver; import java.util.concurrent.TimeUnit; public class HandLinks { public static void main(String[] args) throws InterruptedException { // Initiate the Webdriver WebDriver driver = new ChromeDriver(); // adding implicit wait of 15 secs driver.manage().timeouts().implicitlyWait(15, TimeUnit.SECONDS); // Opening the webpage where we will identify an element driver.get ("https://www.tutorialspoint.com/selenium/practice/links.php"); // identify link with link text locator then click WebElement l = driver.findElement(By.linkText("Created")); l.click(); // identify text locator WebElement t = driver.findElement(By.xpath("/html/body/main/div/div/div[2]/div[1]")); System.out.println("Text appeared is: " + t.getText()); // Closing browser driver.quit(); } }
Output
Text appeared is: Link has responded with status 201 and status text Created Process finished with exit code 0
In the above example, the text obtained after performing the click on the link Created with a message was Link has responded with status 201 and status text Created.
Finally, the message Process finished with exit code 0 was received, signifying successful execution of the code.
Handle Links with Partial Link Text Locator
A link can be identified using the partial link text locator in Selenium. The first element with the matching value of the partial link text is identified.
Syntax
Webdriver driver = new ChromeDriver(); driver.findElement(By.partialLinkText("value of partial link text"));
Example
package org.example; import org.openqa.selenium.By; import org.openqa.selenium.WebDriver; import org.openqa.selenium.WebElement; import org.openqa.selenium.chrome.ChromeDriver; import java.util.concurrent.TimeUnit; public class HandPartialLinks { public static void main(String[] args) throws InterruptedException { // Initiate the Webdriver WebDriver driver = new ChromeDriver(); // adding implicit wait of 15 secs driver.manage().timeouts().implicitlyWait(15, TimeUnit.SECONDS); // Opening the webpage where we will identify an element driver.get("https://www.tutorialspoint.com/selenium/practice/links.php"); // identify link with partial link text locator then click WebElement l = driver.findElement(By.partialLinkText("Creat")); l.click(); // identify text locator WebElement t = driver.findElement(By.xpath("/html/body/main/div/div/div[2]/div[1]")); System.out.println("Text appeared is: " + t.getText()); // Closing browser driver.quit(); } }
Output
Text appeared is: Link has responded with status 201 and status text Created
In the above example, the text obtained after performing the click on the link Created(with the help of partial link text) with a message was Link Link has responded with status 201 and status text Created.
Handle Links with Tagname Locator
A link can be identified using the tagname locator in Selenium. The first element with the matching value of the tagname is identified.
In the example, discussed above once, we had clicked and pointed the arrow to the Created hyperlink, its HTML code was visible, reflecting the anchor tagname (referred to as 'a' and enclosed in <>).
<a href="javascript:void(0);" id="created" onclick="shide('create')">Created</a>
Syntax
Webdriver driver = new ChromeDriver(); driver.findElement(By.tagName("a));
Let us take an example of the same page, where we would first count the total number of links, then we would click on a specific link, say the No Content. After clicking on that link, we would get the text as Link has responded with status 204 and status text on the page.

Example
package org.example; import org.openqa.selenium.By; import org.openqa.selenium.WebDriver; import org.openqa.selenium.WebElement; import org.openqa.selenium.chrome.ChromeDriver; import java.util.concurrent.TimeUnit; import java.util.List; public class TotalLinks { public static void main(String[] args) throws InterruptedException { // Initiate the Webdriver WebDriver driver = new ChromeDriver(); // adding implicit wait of 15 secs driver.manage().timeouts().implicitlyWait(15, TimeUnit.SECONDS); // Opening the webpage where we will identify an element driver.get("https://www.tutorialspoint.com/selenium/practice/links.php"); // identify link with link text locator then click WebElement l = driver.findElement(By.linkText("No Content")); l.click(); // Retrieve all links using locator By.tagName and storing in List List<WebElement> totalLnks = driver.findElements(By.tagName("a") ); System.out.println( "Total number of links: " + totalLnks.size() ) ; // Running loop through list of web elements for( int j = 0; j < totalLnks.size(); j ++){ if( totalLnks.get(j).getText().equalsIgnoreCase("No Content") ) { totalLnks.get(j).click(); WebElement t = driver.findElement(By.xpath("/html/body/main/div/div/div[2]/div[2]")); // get the browser title to confirm navigation after click System.out.println( "Get text after click: " + t.getText()); break ; } } // Closing browser driver.quit(); } }
Output
Total number of links: 42 Get text after click: Link has responded with status 204 and status text No Content
In the above example, we had counted the total number of links on a web page, and received the messages in the console - Total number of links: 42 and the text obtained after performing the the click with the message Get text after click: Link has responded with status 204 and status text No Content.
Conclusion
This concludes our comprehensive take on the tutorial on Selenium Webdriver Handling Links. Weve started with describing how to identify links in HTML, and examples to illustrate how to handle links using the link text, partial link text, and tagname locators in Selenium Webdriver. This equips you with in-depth knowledge of the Selenium Webdriver - Handling Links. It is wise to keep practicing what youve learned and exploring others relevant to Selenium to deepen your understanding and expand your horizons.