How to click on a hyperlink using Selenium WebDriver?
By Sandra Felice, Community Contributor - November 19, 2024
In Selenium WebDriver, you can interact with hyperlinks (which are represented by the <a> tag in HTML) by locating the element using its text or other attributes, and then performing a click action.
Selenium provides built-in methods like By.linkText() or By.partialLinkText() to locate and click hyperlinks.
Steps to Click a Hyperlink in Selenium WebDriver
Below are the steps on how to click on a hyperlink using Selenium WebDriver:
- Initialize WebDriver: First, set up your Selenium WebDriver and navigate to the webpage where the hyperlink is present.
- Locate the Hyperlink: Use the exact or partial text of the hyperlink to find the link element.
- Click the Hyperlink: Use the .click() method to simulate the click on the hyperlink.
- Verify the Navigation: After clicking, you can confirm that the browser has navigated to the correct page by checking the URL.
Example of How to Click a Hyperlink in Selenium WebDriver
This code example will click on a link that leads to www.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 void main(String[] args) { // Set the path 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 using 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 website “https://www.test.com“.
- The hyperlink is located using the exact visible text “Learn more about our services” with By.linkText().
- The .click() method simulates a click on the hyperlink.
- After clicking the link, 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
Additional Notes
- By.linkText() is used when the full text of the link is known. If only part of the link text is available, 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 devices and browsers, it is important to test it in real user conditions.
BrowserStack Automate offers a real device cloud platform, where you can access over 3500+ different devices, browsers, and OS combinations.