Trending October 2023 # Locate Elements By Link Text &Amp; Partial Link Text In Selenium # Suggested November 2023 # Top 15 Popular | Vibergotobrazil.com

Trending October 2023 # Locate Elements By Link Text &Amp; Partial Link Text In Selenium # Suggested November 2023 # Top 15 Popular

You are reading the article Locate Elements By Link Text &Amp; Partial Link Text In Selenium updated in October 2023 on the website Vibergotobrazil.com. We hope that the information we have shared is helpful to you. If you find the content interesting and meaningful, please share it with your friends and continue to follow and support us for the latest updates. Suggested November 2023 Locate Elements By Link Text &Amp; Partial Link Text In Selenium

What is Link Text in Selenium?

A Link Text in Selenium is used to identify the hyperlinks on a web page. It is determined with the help of an anchor tag. For creating the hyperlinks on a web page, we can use an anchor tag followed by the link Text.

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.

Complete Link Text in Selenium – By.linkText()

Code:

import org.openqa.selenium.By; import org.openqa.selenium.WebDriver; import org.openqa.selenium.chrome.ChromeDriver; public class MyClass { public static void main(String[] args) { System.setProperty("webdriver.chrome.driver","G:\chromedriver.exe"); WebDriver driver = new ChromeDriver(); driver.get(baseUrl); System.out.println("title of page is: " + driver.getTitle()); driver.quit(); } }

Here is how it works-

As a result, you will automatically be taken to Google.

Complete Partial Link Text in Selenium – 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.

Code:

import org.openqa.selenium.By; import org.openqa.selenium.WebDriver; import org.openqa.selenium.chrome.ChromeDriver; public class P1 { public static void main(String[] args) { System.setProperty("webdriver.chrome.driver","G:\chromedriver.exe"); WebDriver driver = new ChromeDriver(); driver.get(baseUrl); System.out.println("Title of page is: " + driver.getTitle()); driver.quit(); } }

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.

Code

public static void main(String[] args) { System.setProperty("webdriver.chrome.driver","G:\chromedriver.exe"); WebDriver driver = new ChromeDriver(); driver.get(baseUrl); String theLinkText = driver.findElement(By .partialLinkText("egis")) .getText(); System.out.println(theLinkText); theLinkText = driver.findElement(By .partialLinkText("EGIS")) .getText(); System.out.println(theLinkText); driver.quit(); } Links Outside and Inside a Block

The WebDriver code below accesses both of these links using By.partialLinkText() method.

Code:

import org.openqa.selenium.By; import org.openqa.selenium.WebDriver; import org.openqa.selenium.chrome.ChromeDriver; public class MyClass { public static void main(String[] args) { System.setProperty("webdriver.chrome.driver","G:\chromedriver.exe"); WebDriver driver = new ChromeDriver(); driver.get(baseUrl); System.out.println(driver.getTitle()); driver.navigate().back(); System.out.println(driver.getTitle()); driver.quit(); } }

The output above confirms that both links were accessed successfully because their respective page titles were retrieved correctly.

Summary

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.

You're reading Locate Elements By Link Text &Amp; Partial Link Text In Selenium

Update the detailed information about Locate Elements By Link Text &Amp; Partial Link Text In Selenium on the Vibergotobrazil.com website. We hope the article's content will meet your needs, and we will regularly update the information to provide you with the fastest and most accurate information. Have a great day!