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

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>
Copy

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('https://pythonexamples.org/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>

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>
Copy

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('https://pythonexamples.org/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>

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>
Copy

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('https://pythonexamples.org/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.

Related Tutorials

Quiz on Selenium

Q1. What is Selenium Python used for?

Not answered

Q2. Which of the following is not a popular web driver used in Selenium Python?

Not answered

Q3. What is the method used to find web elements in Selenium Python?

Not answered

Q4. Which of the following is not a commonly used assertion method in Selenium Python?

Not answered

Q5. Which of the following is not a commonly used web element locator in Selenium Python?

Not answered
Code copied to clipboard successfully 👍