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
  • Microsoft Azure Architect Technologies(AZ-300) | ITGuru
    Microsoft Azure Architect Technologies(AZ-300)
  • Azure is a great Microsoft Cloud Computing platform in providing various cloud services through online. ITGuru Certified Azure Architect certification course gives you the practical knowledge on Azure Cloud platform through real-world use cases from live experts

  • ServiceNow Admin Training | ITGuru
    ServiceNow Admin Training
  • Getting knowledge of cloud platforms like ServiceNow is essential in today’s world for the smooth running of projects in cloud platform. Turn your dream to the reality of becoming the Certified ServiceNow Administrator through ServiceNow Administration online certification Course with practical examples by live industry experts through online at ITGuru with real-world use cases.

  • Workday Financials Training | ITGuru
    Workday Financials Training
  • knowing the basics on any platform like Workday is not enough to sustain the IT industry. Hence it is essential to go beyond on Workday basics like Workday Financials training which lets you know the application of Financials management in real -world use cases from ITGuru Live Experts in a practical way.

  • Workday Training
  • An organization is considered as the best one when it offers the best benefits to the employee. Moreover, the greater the employee benefits, the greater the contribution to the organization. ITGuru let you know the practical workday Human Resource Management(HRM) features with live examples by experts

  • Python Online Course | ITGuru
    Python Online Course
  • Turn your dream into reality by ITGuru live experts with real-world use cases through practical knowledge on python online course and become the certified associate in python programming and become a master in python programming

  • Python Programming | ITGuru
    Python Programming
  • Python is the trending programming language in the IT industry. Mastering in python programming gives you more value among the people in the IT industry. Hence start today to learn python programming online by live experts with real-time uses cases at ITGuru


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

Call Now Button