Introduction to Selenium
Selenium WebDriver Basics
WebDriver Commands
Synchronization in Selenium
Working with Different Browsers
Setting up WebDriver for different browsers
Handling Advanced User Interactions
Page Object Model (POM)
Introduction to POM
TestNG Framework
Creating and Running TestNG Tests

In Selenium, locating web elements by link text and partial link text is a common way to interact with hyperlinks (‘<a>‘ tags). Here’s a detailed explanation along with examples:

By Link Text

Using link text to locate a web element means you are using the exact text that appears in the link. This method is case-sensitive and requires the full link text.

Example

Suppose you have the following HTML snippet:

				
					<a href="http://example.com/page1" target="_blank" rel="noopener">Visit Page 1</a>
<a href="http://example.com/page2" target="_blank" rel="noopener">Visit Page 2</a>

				
			

You can locate these links using their full text:

				
					from selenium import webdriver

# Initialize the driver (e.g., Chrome)
driver = webdriver.Chrome()

# Open the webpage
driver.get('http://example.com')

# Locate the link by its text and click it
link = driver.find_element_by_link_text('Visit Page 1')
link.click()

# Close the driver
driver.quit()

				
			

By Partial Link Text

Using partial link text allows you to locate a web element using a portion of the link text. This method is also case-sensitive but is useful when the exact link text is long or dynamic.

Example

Using the same HTML snippet:

				
					<a href="http://example.com/page1" target="_blank" rel="noopener">Visit Page 1</a>
<a href="http://example.com/page2" target="_blank" rel="noopener">Visit Page 2</a>

				
			

You can locate these links using a portion of their text:

				
					from selenium import webdriver

# Initialize the driver (e.g., Chrome)
driver = webdriver.Chrome()

# Open the webpage
driver.get('http://example.com')

# Locate the link by partial text and click it
link = driver.find_element_by_partial_link_text('Page 1')
link.click()

# Close the driver
driver.quit()

				
			

Key Points to Remember

  1. CaseSensivity: Both ‘find_element_by_link_text and find_element_by_partial_link_text‘ are case-sensitive.
  2. Exact Match: ‘find_element_by_link_text‘ requires an exact match of the link text.
  3. Partial Match: ‘find_element_by_partial_link_text‘ can match any substring of the link text, making it more flexible.
  4. Element Type: These methods specifically target ‘<a>‘ elements.

Deprecated Methods Notice

As of recent updates in Selenium (v4.x), the methods ‘find_element_by_link_text and find_element_by_partial_link_text‘ are deprecated. Instead, you should use ‘find_element‘ with ‘By‘:

				
					from selenium import webdriver
from selenium.webdriver.common.by import By

# Initialize the driver (e.g., Chrome)
driver = webdriver.Chrome()

# Open the webpage
driver.get('http://example.com')

# Locate the link by its text and click it
link = driver.find_element(By.LINK_TEXT, 'Visit Page 1')
link.click()

# Locate the link by partial text and click it
link = driver.find_element(By.PARTIAL_LINK_TEXT, 'Page 1')
link.click()

# Close the driver
driver.quit()

				
			

This approach ensures compatibility with the latest versions of Selenium.

By understanding and using these methods, you can effectively interact with links on a webpage, whether you need an exact match or just a partial match of the link text.

Scroll to Top