Web Tables in Selenium

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

The next topic in Selenium it certification is web tables. So now initially, start with

What is a web table?

A table is a HTML data, that is displayed with <table> tag . In the <table> tag <tr> defined the row data and <td> defines the column data. We people usually use <table> to display the data in row and column format. Let us initially have a look regarding the basic HTML table.

<table>
<tbody>
<tr>
<th>Automation Tool</th>
<th>Licensing</th>
<th>Market response</th>
</tr>
<tr>
<td>Selenium</td>
<td>Free</td>
<td>In</td>
</tr>
<tr>
<td>QTP</td>
<td>Paid</td>
<td>Out</td>
</tr>
</tbody>
</table>

This would display the output as shown below

These tables were usually classified as two types :

Static Web tables:

In these tables, the data is static, i.e  here rows and columns are fixed

Dynamic Web tables:

In these tables, data is dynamic. i.e here rows and columns are variable.

So to write the XPath consider the above code

step -1 :

Select the parent Element

In  Web drivers, X-  path locators start with a double forward slash (//) followed by a parent element. Since we are now dealing with tables, the parent will be tables (//tables)

step – 2 :

Add the child Elements:

Since for every table <tbody> is immediately followed by <table> . And <tbody> is usually considered as the child element of <table> . And all child elements will be placed to the right of the parent element <tbody> and these are separated by one forward flash

 Web Tables in Selenium

Add predicates :

From the code shown above, this <tbody> contains two <tr> tags. And these elements were called as the  <tbody> child elements. Moreover, these <tr> elements is called as siblings.

Accessing Nested tables :

A table within a table is called the Nested tables.   In HTML , we can execute the nested tables with the following code

<html>
<head>
<title> Sample</title>
</head>
<body>
<table border="1">
<tbody>
<tr>
<td> One</td>
<td> Two</td>
</tr>
<tr>
<td> three </td>
<td>
<!-- inner table -->
<table border = "1">
<tbody>
<tr>
<td>1-2-3</td>
<td>4-5-6</td>
</tr>
<tr>
<td> 7-8-9</td>
<td>10-11-12</td>
</tr>
</tbody>
</table>
</td>
</tr>
</tbody>
</table>
</body>
</html>

Likewise, through web driver we can execute the following code for nested tables

public static void main(String[] args) {

String baseUrl = ” URL”;

WebDriver driver = new FirefoxDriver();

driver.get(baseUrl);

String innerText = driver.findElement(

By.xpath(“//table/tbody/tr[2]/td[2]/table/tbody/tr/td[2]”)).getText();

       System.out.println(innerText);

driver.quit();

} Web Tables in Selenium

So now we will see

How to use X-path to locate web elements?

Select any dynamic table as shown above. Select any table heading and then right Click on it to inspect the element. Now pick any table heading to add to X-path. And soon after selecting <th>  right click on it then you will be finding a copy option. And then click over it to copy it to the X-path. So like this, people would find X-path and copy it.Web Tables in Selenium

And as the name suggests, these dynamic tales, do not have a limit to rows and Columns. And now, we will see

How to fetch rows and Columns from dynamic web table?

Selenium web driver allows you to fetch the number of rows and columns in the dynamic web table. Execute  the following code to find the rows and columns in your  table.

import java.text.ParseException;
import java.util.List;
//import selenium packages
import org.openqa.selenium.By;
import org.openqa.selenium.WebElement;
//import drivers
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.WebDriver;

public class Noofrowsandcols {
public static void main(String[] args) throws ParseException {
WebDriver wd;
System.setProperty(“webdriver.chrome.driver”,”G://chromedriver.exe”);
wd= new ChromeDriver();
wd.get(” https://onlineitguru.com/onlinetutorials/selenium”);
//No.of Columns
List col = wd.findElements(By.xpath(“.//*[@id=\”leftcontainer\”]/table/thead/tr/th”));
System.out.println(“No of cols are : ” +col.size());
//No.of rows
List rows = wd.findElements(By.xpath(“.//*[@id=’leftcontainer’]/table/tbody/tr/td[1]”));
System.out.println(“No of rows are : ” + rows.size());
wd.close();
}
}

How to fetch the data in the dynamic table?

In the table, we do have numerous amount of data. And to fetch a particular record in the table, scanning it from top to bottom will be a time taking process. So we can directly fetch the data using the row and column number.

import java.text.ParseException;
import java.util.List;
//import selenium packages
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
// import web elements
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
//import time packages
import java.util.concurrent.TimeUnit;

public class RowandCell {
public static void main(String[] args) throws ParseException {
WebDriver wd;
System.setProperty(“webdriver.chrome.driver”,”G://chromedriver.exe”);
wd= new ChromeDriver();
wd.get(“http://demo.guru99.com/test/web-table-element.php”);
wd.manage().timeouts().implicitlyWait(20, TimeUnit.SECONDS);
WebElement baseTable = wd.findElement(By.tagName(“table”));

//To find third row of table
WebElement tableRow = baseTable.findElement(By.xpath(“//*[@id=\”leftcontainer\”]/table/tbody/tr[3]”));
String rowtext = tableRow.getText();
System.out.println(“Third row of table : “+rowtext);

//to get 3rd row’s 2nd column data
WebElement cellIneed = tableRow.findElement(By.xpath(“//*[@id=\”leftcontainer\”]/table/tbody/tr[3]/td[2]”));
String valueIneed = cellIneed.getText();
System.out.println(“Cell value is : ” + valueIneed);
wd.close();
}
}

Asiggnment :

Consider a web table that contains the number values like Salaries.Execute the program to dispaly the max of those numbers.

And if you struck anywhere feel free to contact Selenium Online Courses

And there are some cases,  where the people would like to see the complete data. And you people can see the data through the following code:

import java.text.ParseException;
import java.util.List;
//import selenium packages
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
// import chrome drivers
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.WebElement;
// import timing unit
import java.util.concurrent.TimeUnit;

public class NofRowsColmns {

public static void main(String[] args) throws ParseException {
WebDriver wd;
System.setProperty("webdriver.chrome.driver","G://chromedriver.exe");
wd = new ChromeDriver();
wd.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);
wd.get(URL");
//To locate table.
WebElement mytable = wd.findElement(By.xpath("/html/body/table/tbody"));
//To locate rows of table.
List < WebElement > rows_table = mytable.findElements(By.tagName("tr"));
//To calculate no of rows In table.

int rows_count = rows_table.size();
//Loop will execute till the last row of table.
for (int row = 0; row < rows_count; row++) {
//To locate columns(cells) of that specific row.
List < WebElement > Columns_row = rows_table.get(row).findElements(By.tagName("td"));
//To calculate no of columns (cells). In that specific row.
int columns_count = Columns_row.size();
System.out.println("Number of cells In Row " + row + " are " + columns_count);
//Loop will execute till the last cell of that specific row.
for (int column = 0; column < columns_count; column++) {
// To retrieve text from that specific cell.
String celtext = Columns_row.get(column).getText();
System.out.println("Cell Value of row number " + row + " and column number " + column + " Is " + celtext);
}
System.out.println("-------------------------------------------------- ");
}
}
}

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