Selenium Click Link: Different Ways to Automate Link Clicks
Learn with AI
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:
- How to click a hyperlink using Selenium
- Working examples in both Python and Java
- Ways to locate links using link text, XPath, CSS, and more
- Handling links that open in a new tab or window
- The difference between click and submit
- Common issues and best practices for automating hyperlink clicks
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.
Steps to click a hyperlink in Selenium WebDriver
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.
Example of how to click a hyperlink in Selenium WebDriver
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.
Different ways to locate a hyperlink in Selenium
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.
- By link text: This method matches the exact text between the anchor tags. It works best when the link label is static and clearly defined.
- By partial link text: This option matches a part of the text. It is useful when the full text is long, dynamic, or contains variable content.
- By XPath: Use this when you want to locate the link based on its attributes or hierarchy. You can match href values, parent-child structure, or any attribute combination.
- By CSS selector: This method targets the link using a CSS pattern. It works well when the anchor tag has unique classes or href values.
- By attributes: You can use custom attributes like title, data-id, or even aria-label. These provide flexibility for locating links that are not easily identified by visible text.
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.
Handling links that open in new tabs or windows
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.
Click vs Submit: Key Difference
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.
- The .click() method is ideal for simulating mouse clicks. It works well on hyperlinks, buttons, and interactive elements that respond to user interaction. This is the method you need when testing link-based navigation.
- The .submit() method triggers form submissions. It should be used when the action is tied to an HTML form element. For example, you might locate an input field and call .submit() on its parent form to initiate the process.
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.
Common issues when clicking hyperlinks in Selenium
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.
- ElementNotInteractableException occurs when a link is hidden or covered by an overlay. You can use an explicit wait or scroll the element into view before clicking to avoid this.
- StaleElementReferenceException happens when the page structure changes before the click happens. Re-locate the element right before clicking to ensure you interact with a fresh reference.
- Intercepted clicks happen when popups or ads block the link. Waiting for those elements to disappear or using a more precise locator helps ensure the click succeeds.
- Duplicate link text can lead Selenium to click the wrong element. Use unique locators, index selection, or flags like CSS class or a specific attribute to distinguish the correct link.
To resolve these issues, use one or more of the following techniques:
- Scroll into view using JavaScript to make sure 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 a standard click doesn't respond.
For deeper debugging techniques, see our guide on debugging in Selenium.
Best practices for clicking hyperlinks in Selenium
This checklist helps you build stable and reliable automation scripts. Following these guidelines improves maintainability and test clarity.
- Prefer By.linkText or By.partialLinkText when the link label is clear. It makes locator definitions readable and direct.
- Validate navigation after a click. Always check the page title or URL to confirm that the click worked as expected.
- Use explicit waits before clicking when elements appear dynamically. Waiting for visibility prevents timing mismatches and improves script reliability. See our guide on explicit wait in Selenium for a deeper look at wait strategies.
- Avoid brittle XPath for hyperlinks unless you need to target a specific part of the DOM. Simple locators are easier to maintain.
- Centralize locators in a separate object or module. It simplifies updates when UI changes, and helps teams reuse locator definitions across multiple tests.
These practices make link interactions more reliable and keep your Selenium tests easier to maintain over time.
How Katalon helps you automate better

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.
- Keyword-driven simplicity: Use built-in keywords to interact with UI elements quickly and reliably. Scripts stay clean even as your test suite grows.
- Fast-start learning resource: The free Using Built-in Keywords in Katalon course teaches you how to build tests using test objects and reusable keywords.
- Official reference for click behavior: The Katalon WebUI.click() docs explain how Katalon automatically retries click actions when overlays are present.
- Object Repository Integration: All UI elements are stored in one place. You can reuse them across multiple test cases without writing locators from scratch.
- Cross-browser and mobile support: You can run your click tests across Chrome, Firefox, Safari, Edge, Android, and iOS using the same test logic.
- Low-code test authoring: In Manual Mode, you build tests using drag-and-drop. Then switch to Script Mode for more control when needed.
- Expandable via custom keywords: Create your own keywords when your use case needs a tailored action or extra logic.
- Robust debugging and maintainability: Every test run includes screenshots, logs, and execution history to speed up your troubleshooting.
- CI/CD & reporting integration: Plug Katalon into Jenkins, GitHub Actions, or any CI pipeline. Reports are automatically generated after each run.
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.
|
Frequently asked questions
How do you click a link in Selenium WebDriver?
Locate the anchor element using a locator strategy such as link text, partial link text, XPath, or a CSS selector, then call the click method on it. Selenium simulates the same click behavior a real user would trigger in the browser.
What is the difference between click and submit in Selenium?
The click method simulates a mouse click and works on any interactive element, including hyperlinks and buttons. The submit method only works on elements inside an HTML form and triggers form submission instead of a click event. Calling submit on a hyperlink does nothing, since a link is not a form element.
How do you handle a link that opens in a new tab in Selenium?
Capture the original window handle before clicking the link. After the click, loop through the list of open window handles to find the new one and switch to it. Once you finish working in the new tab, switch back to the original handle to continue the test where it left off.
Why does Selenium click a link but nothing happens?
This usually means the click landed on the wrong element or the element wasn't ready yet. Common causes include the link being hidden behind an overlay, the element becoming stale after the page changed, or the click being intercepted by another element like a popup. Adding explicit waits, scrolling the element into view, or using a more specific locator usually resolves it.
Can Selenium click a hyperlink using XPath instead of link text?
Yes. XPath works well when a link's text is dynamic or when you need to target it based on its attributes or its position in the page structure. Link text and partial link text are usually simpler and more readable when the label is static, so XPath is best reserved for links that don't have a stable, unique text label.
How does Katalon Studio simplify clicking links compared to raw Selenium?
Katalon Studio wraps the same underlying WebDriver click behavior in a built-in keyword that automatically retries the action if an overlay or timing issue blocks the first attempt. It also stores locators in a centralized Object Repository, so teams don't have to hand-write and maintain XPath or CSS selectors for every hyperlink across a test suite.

.png?width=1600&height=420&name=banner9%20(7).png)