How to find Element by Partial Link Text using Selenium?

Find Element by Partial Link Text

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

find_element(By.PARTIAL_LINK_TEXT, "partial_link_text_value")

For an example to link text, in the following code snippet, About can be the partial link text, or 'article', or 'this article' could be partial link text.

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

Examples

In the following example, we find the link element using find_element() function, with partial link text 'New Article'.

The HTML page has two links of which one link has a link text of 'About New Article', and 'New Article' is a partial link text that matches with this link.

HTML Webpage

<html>
 <body>
  <p>Hello World!</p>
  <a href="/tmp/about/">About Hello World</a>
  <br>
  <p>New Article</p>
  <a href="/tmp/new-article/">About New 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-45.html')

# Find element by Partial Link Text
my_link = driver.find_element(By.PARTIAL_LINK_TEXT, 'New Article')
print(my_link.get_attribute("outerHTML"))

# Close the driver
driver.quit()

Output

<a href="/tmp/new-article/">About New Article</a>

If there is no element by given partial link text in the web page, 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 partial link text as 'Policy'. So, when we try to find element by partial link text 'Policy' using find_element() function, then the function throws NoSuchElementException.

HTML Webpage

<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>
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-45.html')

# Find element by Partial Link Text
my_link = driver.find_element(By.PARTIAL_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 partial 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 method to wait for a web element to load in Selenium Python?

Not answered

Q3. What is the method used to navigate back to the previous page in Selenium Python?

Not answered

Q4. What is the method used to switch to a different frame 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 👍