Contents
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>
Example 1: Find link element by partial link text
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.
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 partial link text
myDiv = driver.find_element(By.PARTIAL_LINK_TEXT, 'New Article')
print(myDiv.get_attribute("outerHTML"))
Output
<a href="/new-article/">About New Article</a>
Example 2: No such element for given partial link text
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
.
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.PARTIAL_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 partial link text in the webpage, using Selenium.