Search⌘ K
AI Features

Clicking a Link By Text, ID, and XPath

Explore how to click hyperlinks in Selenium WebDriver using various methods such as full text, partial text, element ID, and XPath. Understand when each approach is suitable, including handling multilingual sites and selecting specific links when duplicates exist.

Click a link by text

Probably the most direct way to click on a link in Selenium is by using text in the web page. For that, we use linkText as follows:

driver.findElement(By.linkText("Recommend Selenium")).click();

Click a link by partial text

We can achieve the similar goal even if we just work with the partial text as shown below:

driver.findElement(By.id("recommend_selenium_link")).click();

Click a link by ID

Although working with the text is very straightforward, there is, ...