How to find Element by Link Text using Selenium?


Find Element by Link Text

To find a Link Element (hyperlinks) by the value (link text) inside the link, using Selenium in Python, call find_element() method and pass By.LINK_TEXT as the first argument, and the link text as the second argument.

find_element(By.LINK_TEXT, "link_text_value")

For an example to link text, in the following code snippet, About this article is the link text.

<a href="/about/">About this article</a>

If there are multiple elements with the same given link text, then only the first of those elements will be returned by find_element() function.

Examples

1. Find element by given link text

In the following example, we have an HTML page with link element whose link text is About this article. In the Python program, we will find this HTML element using find_element() method and link text 'About this article', and print that element to the console.

HTML Webpage

<html>
 <body>
  <p>Hello World!</p>
  <a href="/tmp/about/">About this article</a>
 </body>
</html>

Python Program (Selenium)

from selenium import webdriver
from webdriver_manager.chrome import ChromeDriverManager
from selenium.webdriver.chrome.service import Service as ChromeService
from selenium.webdriver.common.by import By

# Setup chrome driver
driver = webdriver.Chrome(service=ChromeService(ChromeDriverManager().install()))

# Navigate to the url
driver.get('/tmp/selenium/index-43.html')

# Find element by Link Text
my_link = driver.find_element(By.LINK_TEXT, 'About this article')
print(my_link.get_attribute("outerHTML"))

# Close the driver
driver.quit()

Output

<a href="/tmp/about/">About this article</a>

2. There are multiple links in the page with same link text

If there are multiple links with the same link text, then find_element() returns only the first element of those.

HTML Webpage

<html>
 <body>
  <p>Hello World!</p>
  <a href="/tmp/about/">About this article</a>
  <br>
  <p>New Article</p>
  <a href="/tmp/new-article/">About this article</a>
 </body>
</html>

Python Program (Selenium)

from selenium import webdriver
from webdriver_manager.chrome import ChromeDriverManager
from selenium.webdriver.chrome.service import Service as ChromeService
from selenium.webdriver.common.by import By

# Setup chrome driver
driver = webdriver.Chrome(service=ChromeService(ChromeDriverManager().install()))

# Navigate to the url
driver.get('/tmp/selenium/index-44.html')

# Find element by Link Text
my_link = driver.find_element(By.LINK_TEXT, 'About this article')
print(my_link.get_attribute("outerHTML"))

# Close the driver
driver.quit()

Output

<a href="/about/">About this article</a>

3. There is no element for given link text

If there is no element by given link text, find_element() function raises NoSuchElementException.

In the following example, we have taken an HTML page with two links. But, none of the two links have the link text as 'Policy'. So, when we try to find element by link text 'Policy' using find_element() function, then the function throws NoSuchElementException.

HTML Webpage

<html>
 <body>
  <p>Hello World!</p>
  <a href="/tmp/about/">About this article</a>
 </body>
</html>

Python Program (Selenium)

from selenium import webdriver
from webdriver_manager.chrome import ChromeDriverManager
from selenium.webdriver.chrome.service import Service as ChromeService
from selenium.webdriver.common.by import By

# Setup chrome driver
driver = webdriver.Chrome(service=ChromeService(ChromeDriverManager().install()))

# Navigate to the url
driver.get('/tmp/selenium/index-43.html')

# Find element by Link Text
my_link = driver.find_element(By.LINK_TEXT, 'Policy')
print(my_link.get_attribute("outerHTML"))

# Close the driver
driver.quit()

Output

    raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element: {"method":"link text","selector":"Policy"}

Summary

In this tutorial of Python Examples, we learned how to find an element by link text in the webpage, using Selenium.