Links Matching a Criterion
Links can be accessed using an exact or partial match of their link text. The examples below provide scenarios where multiple matches would exist and would explain how WebDriver would deal with them.
In this tutorial, we will learn the available methods to find and access the Links using Webdriver. Also, we will discuss some of the common problems faced while accessing Links and will further discuss on how to resolve them.
Accessing links using Exact Text Match: By.linkText()
Accessing links using their exact link text is done through the By.linkText() method. However, if there are two links that have the very same link text, this method will only access the first one. Consider the HTML code below
When you try to run the WebDriver code below, you will be accessing the first "click here" link
Here is how it works-
As a result, you will automatically be taken to Google.
Accessing links using Partial Text Match: By.partialLinkText()
Accessing links using a portion of their link text is done using the By.partialLinkText() method. If you specify a partial link text that has multiple matches, only the first match will be accessed. Consider the HTML code below.
When you execute the WebDriver code below, you will still be taken to Google.
How to get Multiple links with the same Link Text
So, how to get around the above problem? In cases where there are multiple links with the same link text, and we want to access the links other than the first one, how do we go about it?
In such cases, generally, different locators viz... By.xpath(), By.cssSelector() or By.tagName() are used.
Most commonly used is By.xpath(). It is the most reliable one but it looks complex and non-readable too.
Case-sensitivity for Link Text
The parameters for By.linkText() and By.partialLinkText() are both case-sensitive, meaning that capitalization matters. For example, in Mercury Tours' homepage, there are two links that contain the text "egis" - one is the "REGISTER" link found at the top menu, and the other is the "Register here" link found at the lower right portion of the page.
Though both links contain the character sequence "egis," one is the "By.partialLinkText()" method will access these two links separately depending on the capitalization of the characters. See the sample code below.
Links Outside and Inside a Block
The latest HTML5 standard allows the <a> tags to be placed inside and outside of block-level tags like <div>, <p>, or <h3>. The "By.linkText()" and "By.partialLinkText()" methods can access a link located outside and inside these block-level elements. Consider the HTML code below.
The WebDriver code below accesses both of these links using By.partialLinkText() method.
The output above confirms that both links were accessed successfully because their respective page titles were retrieved correctly.
- Links are accessed using the click() method.
- Apart from the locators available for any WebElement, Links also have link text based locators:
- By.linkText() – locates the links based on the exact match of the link's text provided as a parameter.
- By.partialLinkText() – locates links based on the partial text match of the link's text.
- Both the above locators are case Sensitive.
- If there are multiple matches, By.linkText() and By.partialLinkText() will only select the first match. In such cases where multiple links with the same link text are present, other locators based on xpath, CSS are used.
- findElements() & By.tagName("a") method finds all the elements in the page matching the locator criteria
- Links can be accessed by the By.linkText() and By.partialLinkText() whether they are inside or outside block-level elements.