Link Text & Partial Link Text In Selenium: In our previous post about Java selenium tutorial we have discussed various topics like Locate element By ID, Locate element by Name, Locate element by Class Name and Locate element by Tag Name on a web application. But in this post, we are going to learn about how to locate element by using the link text and partial link text.
In this post, we will try to understand with the real implementation of Link text and Partial Link text for automation testing with selenium. In Selenium WebDriver we have two special types of locators are there which helps us in locating the anchor text elements in a web page. When we are trying to locate link elements in an application then that time we can locate that element with exact match text or with the partial matching text. So using the Link text or partial link text we can able to locate any link elements in a web page.
- How To Locate Element By ID Locator
- How To Locate Element By Name Locator
- How To Locate Element By Class Name Locator
- How To Locate Element By Tag Name Locator
- How To Locate Element By CSS Selector Locator
- How To Locate Element By XPath Locator
Link Text To Locate an Element
If your targeted element is a Link then you can use the Link text locator to locate an element in the web application page. for locating the element we can use the below text:
driver.findElement(By.linkText("this is a link text"));
Note: If there are multiple elements have a sane similar text, then it will automatically select the first one.
Partial Link Text To Locate Element
Partial Link text is another way to locate the link element of a web page. the only difference between link text and partial link text is with the use of partial link text we do not look for the exact text match of the string value, you can locate the element with the partial match also. so it will be helpful when a link has the bigger text so that time you can take the help of partial link text to locate the elements.
the syntax for partial link text is looks something like below:
driver.findElement(By.partialLinkText ("link text"));
Link Text and partial link text of selenium Web Driver work only for handling the link elements of a web application. So in your application, if you want to deal with the link elements then this is the best locator to go with.
If still, you have any queries regarding these two locators then you can comment in the comment section and we are happy to help to clear your queries.