How to click on a hyperlink using Selenium WebDriver?

Use BrowserStack to run Selenium Webdriver Tests on Real Devices for Web Automation Testing.

Get Started free
How to Click on a Hyperlink Using Selenium WebDriver
Home Guide How to click on a hyperlink using Selenium WebDriver?

How to click on a hyperlink using Selenium WebDriver?

In Selenium WebDriver, hyperlinks (HTML <a> tags) can be interacted with by locating the element using its text or attributes and performing a click action. Selenium provides built-in methods like By.linkText() and By.partialLinkText() to easily identify and click links.

Overview

Steps to Click a Hyperlink in Selenium WebDriver

  • Initialize WebDriver → Set up the WebDriver and navigate to the target webpage.
  • Locate the Hyperlink → Use By.linkText(“Full Link Text”) or By.partialLinkText(“Partial Text”).
  • Click the Hyperlink → Use .click() to simulate the click.
  • Verify Navigation → Confirm the browser navigated to the correct URL or page content.

This guide explains how to click hyperlinks in Selenium WebDriver, with examples and best practices for testing across real devices.

Steps to Click a Hyperlink in Selenium WebDriver

Below are the steps on how to click on a hyperlink using Selenium WebDriver:

  1. Initialize WebDriver: First, set up your Selenium WebDriver and navigate to the webpage where the hyperlink is present.
  2. Locate the Hyperlink: Use the exact or partial text of the hyperlink to find the link element.
  3. Click the Hyperlink: Use the .click() method to simulate the click on the hyperlink.
  4. Verify the Navigation: After clicking, you can confirm that the browser has navigated to the correct page by checking the URL.

Example of How to Click a Hyperlink in Selenium WebDriver

This code example will click on a link that leads to www.test.com:

import org.openqa.selenium.By;

import org.openqa.selenium.WebDriver;

import org.openqa.selenium.WebElement;

import org.openqa.selenium.chrome.ChromeDriver;



public class ClickHyperlinkExample {

    public static void main(String[] args) {

        // Set the path for the ChromeDriver executable

        System.setProperty("webdriver.chrome.driver", "path_to_chromedriver");



        // Initialize WebDriver

        WebDriver driver = new ChromeDriver();
        

        try {

            // Navigate to the webpage that contains the hyperlink

            driver.get("https://www.test.com");



            // Locate the hyperlink using its visible text

            WebElement link = driver.findElement(By.linkText("Learn more about our services"));



            // Perform the click action on the hyperlink

            link.click();

            
            // Verify the navigation by printing the current URL

            String currentUrl = driver.getCurrentUrl();

            System.out.println("Navigated to: " + currentUrl);

        } finally {

            // Close the browser after the test

            driver.quit();

        }

    }

}

Explanation:

  • The driver opens the website “https://www.test.com“.
  • The hyperlink is located using the exact visible text “Learn more about our services” with By.linkText().
  • The .click() method simulates a click on the hyperlink.
  • After clicking the link, the browser navigates to the new page. We print the current URL to ensure that the navigation was successful.

Output:

Navigated to: https://www.test.com/services

BrowserStack Automate Banner

Additional Notes

  • By.linkText() is used when the full text of the link is known. If only part of the link text is available, By.partialLinkText() can be used.
  • You can extend the test to verify that the correct page has loaded by comparing the URL or checking for specific content on the new page.

To ensure consistent user experience across different devices and browsers, it is important to test it in real user conditions.

BrowserStack Automate offers a real device cloud platform, where you can access over 3500+ different devices, browsers, and OS combinations.

Talk to an Expert

Tags
Automation Testing Real Device Cloud Selenium Selenium Webdriver

Get answers on our Discord Community

Join our Discord community to connect with others! Get your questions answered and stay informed.

Join Discord Community
Discord