Webdriver Link Recognition

Click to rate this post!
[Total: 0 Average: 0]

In the locator’s section, we have seen regarding the Link text() and partial Link text (). So at that time have you think regarding how to recognize the links? How to handle multiple links? If you got so, this block gives the answer for you.

Link matching Criteria :

Link can be accessed using an exact (or) partial match.  We can access the links in any one of the methods.

Accessing the link through the Exact match():

Through the Exact match, we can access the links using By.LinkText() method. However, if two links having the same text,  each text will open their corresponding links. In HTML, we can do this, through the following code

Code:
<html>
<head>
<title> Sample </title>
</head>
<body>
<a href = "https://onlineitguru.com/"> Click Here </a>
<br>
<a href = "https://onlineitguru.com/blog"> Click Here </a>
</body>
<a>

And you will be achieving  the same through the following code:

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) {
String baseUrl = "https://onlineitguru.com";
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("title of page is: " + driver.getTitle());
driver.quit();
}

}

 Also check how to handle dynamic data in selenium

I hope you people have seen What is partial text in  Locators. Now we will see

How to access the link for Partial Link Text match()?

We can access the partial links (or) a portion of link text using By.PartialLinkText() method. If a specific link text has multiple matches only the first match will be accessed. In HTML we can do this as follows:

<html>
<head>
<title> Partial Match</title>
</head>
<body>
<a href = "https://onlineitguru.com/onlinetutorials/selenium"> Click Here </a>
<br>
<a href = "https://onlineitguru.com/blog"> Visit Here </a>
</body>
<a>

Using selenium driver, we can done the same thing in the following manner:

import org.openqa.selenium.By;

//import drivers
import org.openqa.selenium.WebDriver;

// import browser drivers
import org.openqa.selenium.chrome.ChromeDriver;

public class P1 {

public static void main(String[] args) {
String baseUrl = "http://demo.guru99.com/test/accessing-link.html";
System.setProperty("webdriver.chrome.driver","G:\chromedriver.exe");
WebDriver driver = new ChromeDriver();

driver.get(baseUrl);
driver.findElement(By.partialLinkText("here")).click();
System.out.println("Title of page is: " + driver.getTitle());
driver.quit();
}
}

There are some situation, where we have the same name, but different URL’s. In that case, would you like to know

How to get multiple links with the same text?

Before handling this situation, one thing you should remember that Links are case sensitive, meaning that Register and register are not equal

Webdriver Link Recognition

And one thing, you people should remember that, both By.LinkText and By.partialLinkText() are case sensitive.  And in Selenium web driver, you can do this through the following code

Code:
public static void main(String[] args) { 
String baseUrl = "https://onlineitguru.com/onlinetutorials/selenium"; 
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();

}

A Web page consists of several links. And these links may be hidden (or) visible.  There are some situation, where the user displays would like to know the URL list. Then you can get all the URL's list through the following code.
Code :
package seleniumTutorials;

import java.util.List;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;

public class GetAllLinks {

public static void main(String[] args){
WebDriver driver = new FirefoxDriver();

//Launching sample website
driver.get("http://artoftesting.com/sampleSiteForSelenium.html");
driver.manage().window().maximize();

//Get list of web-elements with tagName - a
List<WebElement> allLinks = driver.findElements(By.tagName("a"));

//Traversing through the list and printing its text along with link address
for(WebElement link:allLinks){
System.out.println(link.getText() + " - " + link.getAttribute("href"));
}

//Commenting driver.quit() for user to verify the links printed
//driver.quit();
} 

}
Assignment:

How to reverse the link list in a webpage?

How to retrieve specific links in a web page?

Get the code for the above links and test your coding skills. And if struck up anywhere feel free to contact it training experts 

Subscribe
to our newsletter

Drop Us A Query

Trending Courses
  • Selenium with python
    Selenium with Python Training
  • As we know, that Selenium with Python Web Browser Selenium Automation is Gaining Popularity Day by Day. So many Frameworks and Tools Have arisen to get Services to Developers.

  • machine learning with python
    Machine Learning with Python Training
  • Over last few years, Big Data and analysis have come up, with Exponential and modified Direction of Business. That operate Python, emerged with a fast and strong Contender for going with Predictive Analysis.

  • Data science with R
    Data Science With R Training
  • Understanding and using Linear, non-linear regression Models and Classifying techniques for stats analysis. Hypothesis testing sample methods, to get business decisions.

  • data science with python
    Data Science with Python Training
  • Everyone starts Somewhere, first you learn basics of Every Scripting concept. Here you need complete Introduction to Data Science python libraries Concepts.

  • devops with azure
    Devops with Azure Training
  • As we Know Azure DevOps is a Bunch of Services, in guiding Developers. It contains CI/CD, pipelines, code Repositories, Visual Reporting Tools and more code management with version control.

  • python training
    Python Certification Training
  • Python is a dynamic interrupted language which is used in wide varieties of applications. It is very interactive object oriented and high-level programming language.


100% Secure Payments. All major credit & debit cards accepted.

Call Now Button