How to click on a hyperlink using Selenium WebDriver?

On This Page Steps to Click a Hyperlink in Selenium WebDriverJune 10, 2026 · 3 min read · Tool Comparison

How to click on a hyperlink using Selenium WebDriver?

In Selenium WebDriver, hyperlinks (HTML & lt; a & gt; tags) can be interacted with by place the element using its textbook or attribute and performing a click action. Selenium provides built-in methods like By.linkText () and By.partialLinkText () to easily name and click links.

Overview

Steps to Click a Hyperlink in Selenium WebDriver

  • Initialize WebDriver→ Set up the WebDriver and navigate to the mark webpage.
  • Locate the Hyperlink→ Use By.linkText (& # 8220; Full Link Text & # 8221;) or By.partialLinkText (& # 8220; Partial Text & # 8221;).
  • Click the Hyperlink→ Use .click () to simulate the click.
  • Verify Navigation→ Confirm the browser navigated to the correct URL or page substance.

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

Steps to Click a Hyperlink in Selenium WebDriver

Below are the steps on how to chatter 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 imitate 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.

Also Read:

Pro tip: Tools like SUSA can handle this autonomously — upload your app and get results without writing a single test script.

Example of How to Click a Hyperlink in Selenium WebDriver

This code example will click on a link that take towww.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 nothingness main (String [] args) {// Set the itinerary 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 utilise 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 site & # 8220;https: //www.test.com“.
  • The hyperlink is place using the exact visible text & # 8220;Learn more about our services& # 8221; withBy.linkText ().
  • The .click()method simulates a click on the hyperlink.
  • After clicking the linkup, 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

Extra Notes

  • By.linkText ()is used when the full schoolbook of the nexus is known. If only component of the nexus text is useable,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 device and browser, it is important to test it in.

BrowserStack Automate offer a program, where you can entree over 3500+ different devices, browsers, and OS combination.

Talk to an Expert

Tags
24,000+ Views

# Ask-and-Contributeabout this topic with our Discord community.

Related Guides

Automate This With SUSA

Upload your APK or URL. SUSA explores like 10 real users — finds bugs, accessibility violations, and security issues. No scripts needed.

Try SUSA Free

Test Your App Autonomously

Upload your APK or URL. SUSA explores like 10 real users — finds bugs, accessibility violations, and security issues. No scripts.

Try SUSA Free