How To Access Links & Tables Using Selenium Webdriver?

We received many request to post articles on Selenium WebDriver and automation framework, so we resumed our Selenium automation testing tutorials from basic to advanced.
 
In this tutorial, we are going to learn about accessing HTML hyperlinks and tables using Selenium WebDriver API.

Accessing Links using Selenium Webdriver:

Hyperlink on a web page has text. Using the text content of the link and based on partial or exact match, the Webdriver API can access that link.

Exact Match:

We can access the hyperlink on a web page as exact match of the text by using By.linkText () method. If there are more than once occurrence of the same text on the same web page then the above method will just access the first occurrence as shown below.

In the following HTML code, it has two hyperlinks with the same text as “click here to view”. However the web page links underlying this text are different. First hyperlink when clicked will route to “https://www.softwaretestingclass.com” and the second one will route to “https://www.linkedin.com/”.

 

<html>
<head>
<title>How to access Links using Selenium Webdriver</title>
</head>
<body>
<a href="https://www.softwaretestingclass.com">click here to view</a><br><br>
<a href="https://www.linkedin.com/">click here to view</a><br><br>
</body>
</html>

 

Following is the Web Page for above HTML code. It can be accessed using the Firefox browser by browsing this link:

[Accessing Links.html]

Following is the Java Code using Webdriver API to access hyperlink by exact match. Here, we are accessing the link by using By.linkText () method of Webdriver API matching link text “click her to view”. Although, there are two such occurrence with the same text but Webdriver will access the first one and click on it to fetch the title of the webpage. Accessed webpage title“Software Testing Classes” is printed on the console as shown below.

package seleniumpackage;

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

public class AccessingLinkByExactMatch {

public static void main(String[] args) {
//declaration and instantiation of objects/variables
WebDriver driver = new FirefoxDriver();
String baseWebUrl = "https://www.softwaretestingclass.com/wp-content/uploads/2017/07/AccessingLinks.html";
String websiteTitle = "";

//Launch Firefox browser and browse the Base URL
driver.get(baseWebUrl);

// get the actual value of the title
driver.findElement(By.linkText("click here to view")).click();
websiteTitle = driver.getTitle();
System.out.println("Title for selected Website: "+websiteTitle);

//closing Firefox Browser and System
driver.close();
System.exit(0);
}
}

Partial Match:

We can access the hyperlink on a web page as partial match of text by using By.partialLinkText () method. If there are more than one occurrence of the same text on the same web page then the above method will just access the first occurrence only as shown below.

In the following HTML code, it has two hyperlinks with the text as “view” and “click here to view”. The web page links underlying these text are “https://www.softwaretestingclass.com/” and route to https://www.linkedin.com/respectively.

Following is the Web Page for above HTML code. It can be accessed using the Firefox browser by browsing this link: [Accessing Links2.html]

<html>
<head>
<title>How to access Links using Selenium Webdriver</title>
</head>
<body>
<a href="https://www.softwaretestingclass.com">view</a><br><br>
<a href="https://www.linkedin.com/">click here to view</a><br><br>
</body>
</html>

Following is the Java Code using Webdriver API to access hyperlink by partial match. Here, we are accessing the link by using By.partiallLinkText () method of Webdriver API matching link text “view”. Although, there are two such occurrences of link (partial match) with the same text yet Webdriver will access the first one and click on it to fetch the title of the webpage. Accessed webpage title “Software Testing Classes” is printed on the console as shown below.

package seleniumpackage;

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

public class AccessingLinkByPartialMatch {

public static void main(String[] args) {
//declaration and instantiation of objects/variables
WebDriver driver = new FirefoxDriver();
String baseWebUrl = "https://www.softwaretestingclass.com/wp-content/uploads/2017/07/AccessingLinks2.html";
String websiteTitle = "";

//Launch Firefox browser and browse the Base URL
driver.get(baseWebUrl);

// get the actual value of the title
driver.findElement(By.partialLinkText("view")).click();
websiteTitle = driver.getTitle();
System.out.println("Title for selected Website: "+websiteTitle);

//closing Firefox Browser and System
driver.close();
System.exit(0);
}
}

Case- sensitivity:

Both accessing link methods By.linkText() and By.PartialLinkText() accepts parameters that are case-sensitive.

In the following HTML code, it has two hyperlinks with the text as “view” and “VIEW”. The web page links underlying these text are “https://www.softwaretestingclass.com/” and route to https://www.linkedin.com/respectively.

<html>
<head>
<title>How to access Links using Selenium Webdriver</title>
</head>
<body>
<a href="https://www.softwaretestingclass.com">view</a><br><br>
<a href="https://www.linkedin.com/">VIEW</a><br><br>
</body>
</html>

Following is the Web Page for above HTML code. It can be accessed using the Firefox browser by browsing this link: [Accessing Links3.html]

Following is the Java Code using Webdriver API to access hyperlink by case sensitive match. Here, we are accessing the link by using By.partiallLinkText () method of Webdriver API matching link text “VIEW” (in upper case). Although, there are two such occurrences of link (lower and upper case) with the same text yet Webdriver will access the second one (“VIEW”)as method parameters are case sensitive and click on it to fetch the title of the webpage. Accessed webpage title “World’s Largest Professional Network | LinkedIn” is printed on the console as shown below.

package seleniumpackage;

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

public class AccessingCaseSensitiveLinks {

public static void main(String[] args) {
// declaration and instantiation of objects/variables
WebDriver driver = new FirefoxDriver();
String baseWebUrl = "https://www.softwaretestingclass.com/wp-content/uploads/2017/07/AccessingLinks3.html";

// Launch Firefox browser and browse the Base URL
driver.get(baseWebUrl);

// get the actual value of the title
driver.findElement(By.partialLinkText("VIEW")).click();
String websiteTitle2 = driver.getTitle();
System.out.println("Title for selected Website for VIEW link: " + websiteTitle2);

// closing Firefox Browser and System
driver.close();
System.exit(0);
}
}

All Links:

If we want to access all the hyperlinks present on a web page then, we can simple gather all the links using By.tagName (“a”) method of Webdriver API as shown below.

List<WebElement> linkOccurence = driver.findElements(By.tagName("a"));

Above statement will return all the web elements into a list which has tag name as “a” which is nothing but the HTML syntax for hyperlink.

In the following Java code, we are using Webdriver API to access all hyperlinks present on a webpage and printing the link texts on the console as shown below.

package seleniumpackage;

import java.util.List;
import java.util.concurrent.TimeUnit;

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

public class AllLinksAccess {
public static void main(String[] args) {
// declaration and instantiation of objects/variables
WebDriver driver = new FirefoxDriver();
String baseWebUrl = "https://www.softwaretestingclass.com/wp-content/uploads/2017/07//AccessingLinks2.html";
driver.manage().timeouts().implicitlyWait(4, TimeUnit.SECONDS);
driver.get(baseWebUrl);

// get the actual value of the title
List<WebElement> linkOccurence = driver.findElements(By.tagName("a"));
for(WebElement ele : linkOccurence){
System.out.println("link occurence: "+ele.getText());
}
// closing Firefox Browser and System
driver.close();
System.exit(0);
}
}

 

Links Outside and Inside a Block:

As we know that with the advent of HTML5 standards, we can now declare <a> tag inside as well as outside of a block level tags such as <div>, <p>, or <h1>. We can still access such inside block and outside block links by using “By.linkText ()” and “By.partialLinkText ()” methods of Webdriver API.

In the following HTML code, first hyperlink is inside the <p> block hence it is inside block link and second hyperlink is outside block hyperlink.

&lt;html&gt;
&lt;head&gt;
&lt;title&gt;How to access Links using Selenium Webdriver&lt;/title&gt;
&lt;/head&gt;
&lt;body&gt;
&lt;p&gt;
&lt;a href="https://www.softwaretestingclass.com"&gt;Inside Block Link&lt;/a&gt;
&lt;/p&gt;
&lt;a href="https://www.linkedin.com/"&gt;
&lt;div&gt;
&lt;span&gt;Outside Block Link&lt;/span&gt;
&lt;/div&gt;
&lt;/a&gt;
&lt;/body&gt;
&lt;/html&gt;

Following is the Web Page for above HTML code. It can be accessed using the Firefox browser by browsing this link: [Outside Inside Links.html]

 

Following is the Java Code using Webdriver API to access hyperlink by partial match for links which are inside and outside of a block. Here, we are accessing the links by using By.partiallLinkText () method of Webdriver API matching link texts “Inside” and “Outside”. Accessed webpage title for inside block link is “Software Testing Classes” and outside block link is “World’s Largest Professional Network | LinkedIn”.These both titles are printed in the console as shown below.

package seleniumpackage;

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

public class OutsideInsideBlockLinks {

public static void main(String[] args) {
// declaration and instantiation of objects/variables
WebDriver driver = new FirefoxDriver();
String baseWebUrl = "https://www.softwaretestingclass.com/wp-content/uploads/2017/07//OutsideInsideLinks.html";
String websiteTitle = "";
String websiteTitle2 = "";
driver.get(baseWebUrl);
driver.findElement(By.partialLinkText("Inside")).click();
websiteTitle = driver.getTitle();
driver.navigate().back();
driver.findElement(By.partialLinkText("Outside")).click();
websiteTitle2 = driver.getTitle();
System.out.println("Title for Inside Block Link Website URL: " + websiteTitle);
System.out.println("Title for Outside Block Link Website URL: " + websiteTitle2);

// closing Firefox Browser and System
driver.close();
System.exit(0);
}
}

Accessing Image Link:

Image link are references to some other web page URL. Since it is not text therefore we cannot use By.linkText() or By.PartialLinkText() methods to access an image link. Instead, we can access image link either By.cssSelector () or By.xpath () methods of Webdriver as shown below.

Following is the Java Code using Webdriver API to access image hyperlink by XPath of the image on a web page. Here, we are accessing the image link by using By.xpath () method of Webdriver API. Accessed webpage title is “World’s Largest Professional Network | LinkedIn” that is printed on the console as shown below.

package seleniumpackage;

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

public class ImageLinkAccess {
public static void main(String[] args) {
// declaration and instantiation of objects/variables
WebDriver driver = new FirefoxDriver();
String baseWebUrl = "https://www.linkedin.com/";
String websiteTitle = "";

// Launch Firefox browser and browse the Base URL
driver.get(baseWebUrl);

// get the actual value of the title
driver.findElement(By.xpath("//html/body/div/div/h1/img")).click();
websiteTitle = driver.getTitle();
System.out.println("Title for selected Website: "+websiteTitle);

// closing Firefox Browser and System
driver.close();
System.exit(0);
}
}

Accessing HTML tables using Selenium Webdriver:

The best to access an element from a HTML table is by XPath.

In the following HTML code, it has a table which has three rows and columns

&lt;html&gt;
&lt;head&gt;
&lt;title&gt;How to access Tables using Selenium Webdriver&lt;/title&gt;
&lt;/head&gt;
&lt;body&gt;
&lt;table name="table" border="2"&gt;
&lt;tr&gt;&lt;td&gt;First Element&lt;/td&gt;&lt;td&gt;First row and second column&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td&gt;Second Element&lt;/td&gt;&lt;td&gt;Second row and second column&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td&gt;Third Element&lt;/td&gt;&lt;td&gt;Third row and second column&lt;/td&gt;&lt;/tr&gt;
&lt;/table&gt;
&lt;/body&gt;
&lt;/html&gt;

Following is the Web Page for above HTML code. It can be accessed using the Firefox browser by browsing this link:

[Accessing Tables.html]

Following is the Java Code using Webdriver API to access HTML table element by XPath. Here, we are accessing a table element which is at second row and second column by using By.xpath () method of Webdriver API. Accessed table element at XPath “//table/tbody/tr[2]/td[2]” is “Second row and second column” that is printed on the console as shown below.

package seleniumpackage;

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

public class AccessingTables {
public static void main(String[] args) {
// declaration and instantiation of objects/variables
WebDriver driver = new FirefoxDriver();
String baseWebUrl = "https://www.softwaretestingclass.com/wp-content/uploads/2017/07/AccessingTables.html";
String text = "";

//Launch Firefox browser and browse the Base URL
driver.get(baseWebUrl);

// get the actual value of the title
text=driver.findElement(By.xpath("//table/tbody/tr[2]/td[2]")).getText();
System.out.println("Select Row and Column text is: "+text);

//closing Firefox Browser and System
driver.close();
System.exit(0);
}
}

Conclusion:

In this chapter, we have learnt to access any hyperlink and table elements present on a webpage.


Checkout more Selenium Training Series Tutorials here.

Leave a Comment

Share This Post