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:
Let’s get started and make link clicking with Selenium feel effortless.
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:
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.
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:
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.
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.
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()
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.
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:
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.
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:
Let’s walk through this with a sample script:
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.
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.
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.
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:
To resolve these issues, use one or more of the following techniques:
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.
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:
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.