Double Bonanza Offer - Upto 30% Off + 1 Self Paced Course Free | OFFER ENDING IN: 0 D 0 H 0 M 0 S

Log In to start Learning

Login via

Post By Admin Last Updated At 2020-06-15
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:
 Sample  Click Here 
Click Here

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:

 Partial Match Click Here 
Visit Here

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

import org.openqa.selenium.By;//import driversimport org.openqa.selenium.WebDriver;// import browser driversimport 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 websitedriver.get("http://artoftesting.com/sampleSiteForSelenium.html");driver.manage().window().maximize();//Get list of web-elements with tagName - aList allLinks = driver.findElements(By.tagName("a"));//Traversing through the list and printing its text along with link addressfor(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