Clicking a link is one of the most common user actions on the web, and one of the most common interactions teams automate in UI tests. In Selenium WebDriver, you can click hyperlinks by locating the anchor element and triggering a browser action that mirrors real user behavior.
With Selenium WebDriver, you can interact with hyperlinks when they are present and interactable, including links that are dynamically loaded or open in a new tab or window. All you need is the right locator and a clear understanding of how the browser responds.
In this article, we’ll walk you through:
In this guide, you’ll learn how to click links in Selenium, choose reliable locator strategies, handle links that open in new tabs, and avoid common click-related issues.
Clicking a link with Selenium WebDriver is a simple process. You launch the browser, find the link, and simulate a user click using the built-in method. This lets your automation follow links just like a real person would.
Step 1: Launch the browser and open the target page. Choose any website you want to test. Selenium will start the session and load the URL.
Step 2: Locate the hyperlink using a Selenium locator strategy. You can use link text, partial link text, XPath, CSS selector, or an attribute like href.
Step 3: Use the click method to simulate interaction. Selenium will trigger the same behavior as a user click.
Step 4: Verify that the link worked. You can check the page title, URL, or the presence of specific content to confirm the navigation was successful.
Here are simple examples in Python and Java that show how to locate a link, click it, and verify that navigation happened successfully.
Python example using link text
from selenium import webdriver
from selenium.webdriver.common.by import By
driver = webdriver.Chrome()
driver.get("https://example.com")
# Click the link using full link text
link = driver.find_element(By.LINK_TEXT, "More information...")
link.click()
# Print the current page title
print(driver.title)
driver.quit()
Java example using partial link text
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
public class ClickLinkExample {
public static void main(String[] args) {
WebDriver driver = new ChromeDriver();
driver.get("https://example.com");
// Click the link using partial link text
driver.findElement(By.partialLinkText("More")).click();
// Print the current page title
System.out.println(driver.getTitle());
driver.quit();
}
}
These examples show two common ways to perform a Selenium click link action using full and partial link text. You can use this approach for most hyperlinks that are clearly labeled on the page.
Before you can click a link in Selenium, you need to identify the hyperlink accurately. Selenium provides several ways to do this, and each has its own advantages depending on the structure of the page.
For a full breakdown of every locator type, see our guide on locators in Selenium, or our dedicated guide on XPath in Selenium for more detailed XPath techniques.
When clicking links in Selenium, choose the most unique and stable locator available. This keeps your test scripts reliable across page changes and layout updates.
Some hyperlinks open content in a new tab or window instead of the same view. In those cases, your script needs to shift focus and return later with a clean flow that mirrors real user behavior.
Step 1: Capture the current window’s identifier using getWindowHandle().
Step 2: After clicking the link, collect all open window handles using getWindowHandles(). This gives you access to the new tab or window.
Step 3: Switch context to the new window using switchTo().window(handle).
Step 4: Perform any checks or actions within the new window. Then close it or switch back to the original using its handle.
Here is a Python example illustrating the flow:
from selenium import webdriver
from selenium.webdriver.common.by import By
driver = webdriver.Chrome()
driver.get("https://katalon.com")
original = driver.current_window_handle
driver.find_element(By.LINK_TEXT, "Open in new tab").click()
for handle in driver.window_handles:
if handle != original:
driver.switch_to.window(handle)
print("New tab title:", driver.title)
driver.close()
break
driver.switch_to.window(original)
print("Back to original title:", driver.title)
driver.quit()
This pattern ensures your script stays in sync with the browser context. It mirrors user behavior of opening a link, verifying content, and returning to the starting point, creating tests that are both reliable and understandable. For a deeper look at multi-tab flows, see our guide on how to switch tabs in Selenium for Python.
In Selenium automation, both .click() and .submit() simulate user actions, but they serve distinct purposes. Understanding when to use each one makes your test cases cleaner and more predictable.
Here’s a helpful tip: links are not form elements, so calling .submit() on a hyperlink won't trigger navigation. Stick with .click() when your goal is to follow a link or press a button outside a form.
Keeping this distinction in mind will help you write clearer Selenium tests and avoid unnecessary click or form-submission errors.
Even simple actions like simulating a link click with Selenium can encounter unexpected challenges. Understanding common problems and effective debugging techniques will help your automation run smoothly every time.
To resolve these issues, use one or more of the following techniques:
For deeper debugging techniques, see our guide on debugging in Selenium.
This checklist helps you build stable and reliable automation scripts. Following these guidelines improves maintainability and test clarity.
These practices make link interactions more reliable and keep your Selenium tests easier to maintain over time.
Selenium gives you low-level browser control, but teams still need practical workflows for object management, faster test authoring, consistent execution, and result review. Katalon Studio builds on Selenium with built-in keywords such as WebUI.click(), a centralized Object Repository, and integrated execution and reporting workflows. That means teams can automate link interactions faster and maintain their tests with less framework overhead.
Instead of assembling your automation framework piece by piece, you can start with built-in project structure, reusable test objects, execution support, and reporting out of the box.
If your team is already using Selenium for basic UI interactions such as clicking links, Katalon can help you scale that automation with faster authoring, better maintainability, and built-in reporting.