Webdriver Link Recognition

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
  • Pega testing training
    PEGA Testing Training
  • Today application testing is the deciding factor to launch the application into the market. And people do not launch the application unless it goes true.

  • blockchain course
    Blockchain Online Course
  • Today many people were enthusiastic, to know the exact details of things happening around him. This can get the proper knowledge on Blockchain.

  • ethical hacking course
    Ethical Hacking Certification Course
  • Zeal to learn ethical hacking is common among college students and IT professionals. Because everybody wants to secure their system from cyber attacks.

  • python training
    Python Online 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.

  • Tableau Training
    Tableau Training
  • Tableau is a Software company that caters interactive data visualization products that provide Business Intelligence services. The company’s Head Quarters is in Seattle, USA.

  • what is pega
    PEGA Online Training
  • Pega Systems Inc. is a Cambridge, Massachusetts based Software Company. It is known for developing software for Customer Relationship Management (CRM) and Business process Management (BPM).


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