The Katalon Blog

How To Click Hyperlinks in Selenium WebDriver

Written by Vincent N. | Sep 26, 2025 11:30:02 AM

Hyperlinks connect users to pages, actions, and content. In web testing, they are everywhere. That’s why knowing how to click a hyperlink in Selenium WebDriver is one of the first and most essential skills for any test automation engineer.

Selenium makes it simple, but real-world links come with variations. Some are plain anchor tags. Some use JavaScript. Others open new tabs or redirect dynamically. Each requires a slightly different approach.

In this article, we'll walk through:

  • How to click a hyperlink in Selenium WebDriver step by step
  • Examples in Java and Python to show exactly how it works
  • Ways to locate hyperlinks using link text, XPath, and CSS
  • What to do when links open in new tabs or windows
  • Best practices for reliable hyperlink automation
  • Common issues and how to debug them quickly

Let’s get started and make link clicking with Selenium feel effortless.

Different Ways to Locate a Hyperlink in Selenium

Locating the right element is key to automation. When you want to click hyperlink in Selenium WebDriver, you have a few different locator strategies available. Each method offers a unique way to target the link based on how it’s written in the page source.

Here are some of the most common and effective ways to find a hyperlink:

  • By Link Text
    This method works best when the link text is exact and stable. Selenium matches the full anchor text to locate the link.
  • By Partial Link Text
    Use this when the link text is long or dynamic. You can target just a portion of the text to identify the link quickly.
  • By XPath
    XPath helps when the link is nested inside other elements or when the page structure is complex. You can write precise expressions to locate what you need.
  • By CSS Selector
    CSS selectors give you control when you want to match by class, ID, or attribute. They are useful for targeting links with specific visual styles or parent elements.
  • By Attributes
    You can locate links using attributes like href or target. This works well when other selectors are unavailable or less reliable.

Each of these methods helps you automate interactions cleanly. The best choice depends on the structure of your page and how stable the HTML is over time.

When writing tests that click hyperlinks in Selenium, a strong locator strategy gives you fewer issues and better test accuracy.

For more detailed techniques, visit our guide on XPath in Selenium.

Steps to Click a Hyperlink in Selenium WebDriver

Clicking a link is one of the simplest actions in test automation. With Selenium WebDriver, it becomes a clear four-step process that works across browsers and frameworks.

Here’s the standard approach to click a hyperlink in Selenium WebDriver:

  1. Launch the browser and navigate to the page
    Create a WebDriver instance and open the URL where the hyperlink lives. This sets the test environment.
  2. Identify the hyperlink using a locator
    Use a reliable locator strategy. Link text and partial link text work well for most anchor tags. You can also use XPath, CSS selectors, or attributes like href if needed.
  3. Use the click() method to trigger the action
    Once located, call the click command on the element. Selenium will simulate a user click.
  4. Verify the result
    Check if the navigation was successful. You might validate the new page’s URL, title, or the presence of a known element.

This flow helps automate any interaction involving links. Whether it’s a login button, help page, or a navigation tab, you can apply this same structure. As you build more scripts, this method becomes second nature.

Insight: Every link is a point of user intent. Automation works best when it captures that clearly and consistently.

Example of How to Click a Hyperlink in Selenium WebDriver

Let’s look at a real example. In this case, we’ll click the “Contact Us” link on https://katalon.com. You’ll see how to locate the link, click it, and verify that the navigation works as expected. We’ll use both Java and Python so you can follow along in your preferred language.

Clicking a hyperlink using link text

Python
from selenium import webdriver
from selenium.webdriver.common.by import By

driver = webdriver.Chrome()
driver.get("https://katalon.com")

contact_link = driver.find_element(By.LINK_TEXT, "Contact Us")
contact_link.click()

assert "contact" in driver.current_url
driver.quit()

Clicking a hyperlink using Java

Java
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;

public class ClickLinkExample {
  public static void main(String[] args) {
    WebDriver driver = new ChromeDriver();
    driver.get("https://katalon.com");

    WebElement contactLink = driver.findElement(By.linkText("Contact Us"));
    contactLink.click();

    assert driver.getCurrentUrl().contains("contact");
    driver.quit();
  }
}

Each script follows the same flow. It opens the browser, finds the “Contact Us” link, clicks it, and verifies that the page changed. This pattern works across most types of links you’ll encounter.

Use this as a base for other scenarios where you need to click hyperlink in Selenium WebDriver on different pages and platforms.

Click vs Submit: Understanding the Difference

Both click() and submit() are used to trigger actions in Selenium. They work differently, and knowing when to use each one is important for test accuracy.

The click() method simulates a real user clicking on a button, link, or image. It is the preferred choice when you want to click hyperlink in Selenium WebDriver. This method works well for navigation, opening new views, or activating any UI element tied to an event handler.

The submit() method is used for submitting forms. You can use it when an input field belongs to a form tag. When the field is part of a login or contact form, submit() triggers the form submission without needing to find the submit button directly.

Here’s a simple example of both methods:

Python
from selenium import webdriver
from selenium.webdriver.common.by import By

driver = webdriver.Chrome()
driver.get("https://katalon.com")

# Click a regular hyperlink
driver.find_element(By.LINK_TEXT, "Contact Us").click()

# Submit a form from an input field
driver.find_element(By.NAME, "email").submit()

driver.quit()

Hyperlinks usually do not need submit(). When the element is not inside a form, always choose click() to interact. This keeps your scripts aligned with how the browser would behave.

When you click hyperlinks in Selenium WebDriver, your goal is to mimic the user’s action. click() handles that with clarity and precision every time.

Handling Links that Open in New Tabs or Windows

Selenium provides full control over multiple windows. You can capture all open window handles, switch between them, and verify what’s inside each one. This is especially useful for links that launch external pages.

Here’s a simple workflow you can use:

  1. Click the link that opens a new tab or window.
  2. Capture window handles using the WebDriver session.
  3. Switch to the new window using the handle index.
  4. Verify the page by checking the title or a key element.

Let’s walk through this with a sample script:

Python
from selenium import webdriver
from selenium.webdriver.common.by import By

driver = webdriver.Chrome()
driver.get("https://katalon.com")

# Click a link that opens a new tab
driver.find_element(By.LINK_TEXT, "Privacy Policy").click()

# Capture all open window handles
windows = driver.window_handles

# Switch to the second window
driver.switch_to.window(windows[1])

# Verify the page
print(driver.title)

# Close the new window and return to the main one
driver.close()
driver.switch_to.window(windows[0])

driver.quit()

Using this technique keeps your test scripts consistent even when the browser opens additional windows. It gives you full visibility across sessions and helps verify the complete user experience.

When working with links that open new tabs, this kind of Selenium link click logic becomes a core part of test flow design.

Best Practices for Clicking Hyperlinks in Selenium

Strong automation depends on reliable steps. When you click hyperlink in Selenium WebDriver, using stable locators and clear test flows keeps your scripts consistent. Below is a checklist to guide your test design.

  • Use By.linkText or By.partialLinkText
    These are simple and effective for most hyperlink interactions.
  • Choose descriptive locators
    Look for link text or attributes that reflect the element’s role in the UI.
  • Favor locator stability
    Prefer selectors that change less often during development cycles.
  • Validate navigation after clicking
    Always check that the page loads as expected or that the intended element is visible.
  • Centralize locator definitions
    Store them in one place. This makes test maintenance faster and less error-prone.

These best practices make every Selenium link click test cleaner and easier to debug. The goal is not just to click the link but to trust the result every time it runs.

Common Issues when Clicking Links in Selenium

Sometimes a link looks ready but doesn’t respond to a click right away. When using click hyperlink in Selenium WebDriver, it helps to know what can delay or block the action. These issues are common in dynamic web environments and can be resolved with the right tools.

Here are some conditions that may require extra attention:

  • Link behind overlays or modals
    Visual layers like cookie banners or popups may cover the link. In these cases, you can close the overlay first or wait until it disappears.
  • Link not clickable during load
    Some links are added to the DOM after a delay. You can wait for visibility or use expected conditions to ensure readiness.
  • Element click intercepted
    This happens when another element overlaps the link at the time of the action. Adjusting scroll position or waiting for the UI to stabilize can help.
  • Duplicate link text
    If the same link label appears in multiple places, you can locate it using position, parent class, or attribute-based filters.

To resolve these issues, use one or more of the following techniques:

  • Scroll into view using JavaScript to ensure the element is within the visible area.
  • Use the Actions class to simulate more precise user behavior, especially for hover-based interactions.
  • Trigger the click with JavaScript when the element is detected but standard click does not respond.

When you understand these patterns, every Selenium link click becomes smoother. Debugging becomes faster because you know where to look and what to apply.

For deeper debugging techniques, visit our guide on debugging in Selenium.

Why choose Katalon to automate tests?

Katalon is built on top of Selenium and brings a full set of capabilities to help teams test faster and smarter. It gives you all the power of WebDriver with a clean interface, smart test features, and strong scalability. Whether you work with scripts or prefer a visual flow, Katalon adapts to your needs.

When you want to click hyperlink in Selenium WebDriver, Katalon simplifies the setup and gives you built-in tools for better stability and visibility. The platform reduces the time you spend writing boilerplate code and gives you more time to focus on test quality.

Here are key advantages that make Katalon the preferred choice for test automation:

  • Unified Platform: bring together test design, test suites, execution, reports, and analytics in one place
  • Cross-Browser and Cross-Platform: test on thousands of browser and OS combinations without managing drivers
  • Scalability: run tests in parallel, locally or in the cloud, and trigger them directly from CI/CD pipelines
  • Smart Test Maintenance: AI-powered self-healing locators adapt to UI changes and reduce test flakiness
  • Built-in Test Management and Analytics: visualize test results with dashboards, charts, and trend analysis

These features make it easy to scale your automation while keeping it clean and reliable. You can spend less time troubleshooting and more time improving test coverage.

Katalon helps you go beyond writing scripts. It gives you control, visibility, and speed all in one platform. You can still use the full flexibility of Selenium while gaining features that make your automation lifecycle complete.

Start exploring with Katalon Docs for practical examples or join Katalon Academy to level up your testing skills.

📝 Want to explore Katalon with your team? Request a demo and see how it improves every step of your test automation.