
➡️Mastering Selenium WebDriver:
1.Master Selenium WebDriver by learning how to handle links using Link Text and Partial Link Text.
2.Improve your automation testing skills with Selenium’s Link Text and Partial Link Text locators.
3.Learn to efficiently locate and interact with web elements using Link Text and Partial Link Text.
4.Optimize your Selenium scripts by selecting the right locators for web automation.
5.Discover how Link Text and Partial Link Text improve test script reliability.
6.Enhance your test automation framework with effective link handling techniques.
7.Boost your Selenium proficiency by mastering different element locators.
How to Click Image Links Using CSS Selectors:-
➡️Introduction:
When automating web applications using Selenium WebDriver, identifying and interacting with hyperlinks is a fundamental task. Selenium provides several locators to find elements, and for hyperlinks, By.linkText() and By.partialLinkText() are specifically designed to locate links using their textual content. This article delves into their usage, limitations, and best practices for handling multiple links with similar text.
➡️Understanding Link Text in Selenium:
Link text refers to the visible, clickable text within an anchor (<a>
) tag on a webpage. Selenium provides two methods to locate elements using their text:
- By.linkText() – Locates elements by matching the exact link text.
- By.partialLinkText() – Locates elements by matching a portion of the link text.
➡️Using By.linkText() in Selenium:
The By.linkText() method is useful when the exact text of a hyperlink is known. However, if multiple links have the same text, only the first occurrence will be selected.
Example:
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
public class LinkTextExample {
public static void main(String[] args) {
String baseUrl = "http://example.com/test";
System.setProperty("webdriver.chrome.driver", "G:\\chromedriver.exe");
WebDriver driver = new ChromeDriver();
driver.get(baseUrl);
driver.findElement(By.linkText("Click Here")).click();
System.out.println("Page Title: " + driver.getTitle());
driver.quit();
}
}
➡️Using By.partialLinkText() in Selenium:
When only a portion of the link text is known, By.partialLinkText() is a useful alternative. It finds the first matching link containing the given text fragment.
Example:
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
public class PartialLinkTextExample {
public static void main(String[] args) {
String baseUrl = "http://example.com/test";
System.setProperty("webdriver.chrome.driver", "G:\\chromedriver.exe");
WebDriver driver = new ChromeDriver();
driver.get(baseUrl);
driver.findElement(By.partialLinkText("Click")).click();
System.out.println("Page Title: " + driver.getTitle());
driver.quit();
}
}
➡️Handling Multiple Links with the Same Text:
- Link Text and Partial Link Text help in identifying hyperlinks dynamically in web pages.
- Reduce XPath dependency by leveraging Link Text and Partial Link Text for element identification.
- Achieve seamless navigation in your test scripts with Selenium WebDriver’s link-handling features.
If multiple links share the same text, Selenium selects only the first match. To handle this, alternative locators like By.xpath(), By.cssSelector(), or By.tagName() can be used.
➡️Mastering Selenium WebDriver Using XPath:
driver.findElement(By.xpath("(//a[text()='Click Here'])[2]")).click();
➡️Mastering Selenium WebDriver Using CSS Selector:
driver.findElement(By.cssSelector("a[href='second_link.html']")).click();
➡️Using findElements() to Retrieve All Matching Links:
List<WebElement> links = driver.findElements(By.linkText("Click Here"));
links.get(1).click();
➡️Case Sensitivity in Link Text Identification:
Both By.linkText() and By.partialLinkText() are case-sensitive. If capitalization differs, the element may not be found.
Example:
System.out.println(driver.findElement(By.partialLinkText("Register"))); // Works
System.out.println(driver.findElement(By.partialLinkText("register"))); // May not work
➡️Accessing Links Inside & Outside Block Elements:
- Master the art of handling web links efficiently using Selenium’s built-in locators.
Modern HTML5 allows anchor tags within block-level elements like <div>
, <p>
, or <h3>
. Selenium can locate these links effectively.
Example:
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
public class BlockLevelLinks {
public static void main(String[] args) {
String baseUrl = "http://example.com/test/block";
System.setProperty("webdriver.chrome.driver", "G:\\chromedriver.exe");
WebDriver driver = new ChromeDriver();
driver.get(baseUrl);
driver.findElement(By.partialLinkText("Inside")).click();
System.out.println(driver.getTitle());
driver.navigate().back();
driver.findElement(By.partialLinkText("Outside")).click();
System.out.println(driver.getTitle());
driver.quit();
}
}
➡️Mastering Selenium WebDriver Summary:
- Selenium provides By.linkText() and By.partialLinkText() for locating hyperlinks by their text.
- By.linkText() requires an exact match, while By.partialLinkText() matches a substring of the link text.
- Both methods are case-sensitive and will only select the first matching link if duplicates exist.
- To handle multiple links with the same text, XPath, CSS selectors, or findElements() can be used.
- Links inside and outside block elements are accessible using these locators.
By mastering these techniques, testers can efficiently locate and interact with hyperlinks, making Selenium automation more effective and robust.