When it comes to automating and testing web applications in our tech-savvy world, we can’t underestimate the importance of getting things right. That’s where Selenium, the go-to automation framework, comes into play, offering developers and testers a bunch of powerful tools to tame web browsers. As we all know, hyperlinks play a big role in the composition of any web page. In order to successfully locate these links, we have a secret weapon: the find_element() method with Link Text in Selenium.
Understanding how “find_element()” by Link Text works
The find_element() method with Link Text in Selenium is a powerful tool for locating elements on a web page based on their visible text content. This method allows Selenium to search through the HTML document and identify elements that have the exact visible text specified.
To understand how this method works, let’s consider an example using HTML. Suppose we have a web page with a navigation menu containing various links. Here is a snippet of the HTML code:
<nav>
<ul>
<li><a href="#">Home</a></li>
<li><a href="#">Products</a></li>
<li><a href="#">About Us</a></li>
<li><a href="#">Contact</a></li>
</ul>
</nav>
If we want to locate the element with the visible text “About Us”, we can use the selenium find_element() method with Link Text. Selenium will search for an anchor tag <a>
that exactly matches the provided link text.
By utilizing this method, Selenium will find the specific element that contains the visible text “About Us”. This approach disregards any underlying HTML structure or attributes and focuses solely on the visible text of elements.
In the next section we will explore how to use this method.
Important Note: Starting from Selenium version 4.3.0, there have been significant changes to the find_element()
method and its related functions. Specifically, the find_element_by_link_text()
method, along with other similar functions, has been deprecated and replaced by a unified find_element()
function.
To use the find_element()
method with link text, we need to pass By.LINK_TEXT
as the first parameter. Let’s take a look at the syntax:
element = driver.find_element(By.LINK_TEXT, **)
Syntax and Usage
The find_element()
method is a crucial component of Selenium, allowing developers to locate elements on a web page based on their link text. However, it’s important to note that the name of the function itself doesn’t explicitly indicate its association with link text. In reality, this method offers the capability to search for elements using various techniques, extending beyond just Link Text.
To specifically search by link text, it is necessary to pass the correct locator as the first parameter of the function, as shown below:
element = driver.find_element(By.LINK_TEXT, )
This requires an additional import of “By” to run:
from selenium.webdriver.common.by import By
We also need to setup our webdriver before we begin. Here is our setup code, which will be re-using throughout the rest of the article.
from webdriver_manager.chrome import ChromeDriverManager
from selenium.webdriver.chrome.service import Service as ChromeService
from selenium import webdriver
from selenium.webdriver.common.by import By
html="""sample html"""
service = ChromeService(executable_path=ChromeDriverManager().install())
options = webdriver.ChromeOptions()
options.add_argument('--headless=new')
driver = webdriver.Chrome(options=options, service=service)
driver.get("data:text/html;charset=utf-8,{html_content}".format(html_content=html))
driver.quit()
Examples using find_element() by Link Text in Selenium
Let’s explore five different examples that showcase various concepts of using the find_element() method with link text.
Example 1: Simple Link Text
Consider the following HTML snippet:
<a href="#">Click me</a>
To locate the link element with the text “Click me,” we can use the following code:
element = driver.find_element("link_text", "Click me")
The code searches for an <a>
element with the exact link text “Click me” and assigns it to the element
variable.
Example 2: Partial Link Text
Suppose we have the following HTML snippet:
<a href="#">Learn more about Selenium WebDriver</a>
To locate the link element with the partial link text “Selenium WebDriver,” we can use the following code:
element = driver.find_element("partial_link_text", "WebDriver")
The code searches for an <a>
element containing the partial link text “WebDriver” and assigns it to the element
variable. This approach is useful when the complete link text is not known or may vary.
Example 3: Case Sensitivity
Consider the following HTML snippet:
<a href="#">click Me</a>
<a href="#">Click Me</a>
To locate the link element with the link text “Click Me” we can use the following code:
element = driver.find_element("link_text", "Click Me")
For locating the element with link text “click me”, we can do:
element = driver.find_element("link_text", "click Me")
By default, the find_element()
method performs a case sensitive search. Regretfully, this behavior cannot be changed. As of Selenium version 4.9.0, there is no easy way of over-coming this issue.
Your only options are to combine multiple functions together (each checking a different case version), or to setup XPath 2.0 on your system, which has a function which handle this particular case.
Example 4: Multiple Elements with the Same Link Text
Suppose we have the following HTML snippet:
<a href="#">Click me</a>
<a href="#">Click me</a>
To locate all the link elements with the text “Click me,” we can use the following code:
elements = driver.find_elements("link_text", "Click me")
Instead of using find_element()
, we use find_elements()
to retrieve a list of all matching elements with the specified link text. In this case, both <a>
elements with the text “Click me” will be returned in the elements
list.
Example 5: Complex Link Text
Consider the following HTML snippet:
<div>
<a href='/'>
<span>Click me</span>
</a>
</div>
To locate the link element with the complex link text “Click me,” including nested tags, we can use the following code using XPath expressions.
element = driver.find_element(By.XPATH, "//a[span[text()='Click me']]")
print(element.get_attribute('outerHTML'))
<a href="/">
<span>Click me</span>
</a>
In cases where the link text is embedded within other HTML elements, we can utilize XPath to locate the desired element. In the above code, the XPath expression searches for a <span>
element within an <a>
element containing the text “Click me” anywhere within its structure.
As you might have guessed, XPath is usually preferred in complex situations.
These examples demonstrate the versatility of the find_element() method in locating elements using their link text in Selenium. By understanding these concepts, you can effectively identify and interact with specific elements on web pages using Selenium.
This marks the end of the Selenium find_element() with Link Text Tutorial. Any suggestions or contributions for CodersLegacy are more than welcome. Questions regarding the tutorial content can be asked in the comments section below.