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.

Scenario 1: Find element by 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.

index.html

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

Python Program (Selenium)

from selenium.webdriver.chrome.service import Service
from selenium import webdriver
from selenium.webdriver.common.by import By

service = Service(executable_path="/usr/local/bin/chromedriver")
#initialize web driver
with webdriver.Chrome(service=service) as driver:
    #navigate to the url
    driver.get('http://127.0.0.1:5500/localwebsite/index.html')
    #find element by link text
    myDiv = driver.find_element(By.LINK_TEXT, 'About this article')
    print(myDiv.get_attribute("outerHTML"))

Output

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

Scenario 2: 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.

index.html

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

Python Program (Selenium)

from selenium.webdriver.chrome.service import Service
from selenium import webdriver
from selenium.webdriver.common.by import By

service = Service(executable_path="/usr/local/bin/chromedriver")
#initialize web driver
with webdriver.Chrome(service=service) as driver:
    #navigate to the url
    driver.get('http://127.0.0.1:5500/localwebsite/index.html')
    #find element by link text
    myDiv = driver.find_element(By.LINK_TEXT, 'About this article')
    print(myDiv.get_attribute("outerHTML"))

Output

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

Scenario 3: No such 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.

index.html

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

Python Program (Selenium)

from selenium.webdriver.chrome.service import Service
from selenium import webdriver
from selenium.webdriver.common.by import By

service = Service(executable_path="/usr/local/bin/chromedriver")
#initialize web driver
with webdriver.Chrome(service=service) as driver:
    #navigate to the url
    driver.get('http://127.0.0.1:5500/localwebsite/index.html')
    #find element by link text
    myDiv = driver.find_element(By.LINK_TEXT, 'Policy')
    print(myDiv.get_attribute("outerHTML"))

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.