Partial Link Text Locators


In some situations, we need to find links by a portion of the text in a Link Text element. In such situations, we use Partial Link Text to locate elements.
Ex: Logout<user name>
Here Logout link text is static, but user name is keep on changing where ever new user login. In this situation we will use Partial Link Text Locator instead of Link Text Locator.

 Scenario :
Click on the Data Policy Link.

Following is the steps to locate the element using Partial Link on webpage.

    Right click on Data Policy webelement on the page and select inspect option in the context menu

Pick the partial link text


Syntax for locating a web element using its Partial Link Text is written as:

driver.find_element(By.PARTIAL_LINK_TEXT,<linktext>)
or
driver.find_element_by_partial_link_text(<linktext>)

Script:

from selenium import webdriver
from selenium.webdriver.common.by import By
chromePath=
"C:\\BrowserDrivers\\chromedriver.exe"

driver=webdriver.Chrome(executable_path=chromePath)
driver.get(
"https://www.facebook.com/")

driver.find_element_by_partial_link_text(
"Data").click()


Note:
Case-sensitivity for Link Text/Partial Link Text

The parameters for link_text () and partial_link_text() are both case-sensitive,
For example, there are two links that contain the text, one is the "REGISTER" link found (All letters are capital), and the other is the "Register" link found on the same page. Now you want to click on “REGISTER” Link then you should give like this
driver. find_element_by_link_text("REGISTER").click()
If you give driver. find_element_by_link_text("Register").click()  then it will click on another link  not on “REGISTER” Link.


Comments