Open In App

How to Handle Static Web Tables using Selenium WebDriver using Java?

Last Updated : 11 Apr, 2022
Improve
Improve
Like Article
Like
Save
Share
Report

Web Tables are used to organize similar types of content on the web page, Web tables are groups of elements that are logically stored in a row and column format. Web table has various HTML tags like., table, th, tr, td. Let’s understand these tags a bit more:

  • <table> – It defines a table
  • <th> – It defines a header cell
  • <tr> – It defines a row in a table. 
  • <td>- It defines a cell in a table. the <td> tag always lies inside the <tr> tag.

Example of HTML Table

Static Demo Table

The below table is a static demo table and the HTML code for the table:

HTML




<html>
   <head>
      <style></style>
   </head>
   <body>
      <table name="BookTable">
         <tr>
            <th>BookName</th>
            <th>Author</th>
            <th>Subject</th>
            <th>Price</th>
         </tr>
         <tr>
            <td>Learn Selenium</td>
            <td>A</td>
            <td>Selenium</td>
            <td>100</td>
         </tr>
         <tr>
            <td>Learn Java</td>
            <td>B</td>
            <td>Java</td>
            <td>500</td>
         </tr>
         <tr>
            <td>Learn JS</td>
            <td>C</td>
            <td>Javascript</td>
            <td>700</td>
         </tr>
         <tr>
            <td>Master In Selenium</td>
            <td>D</td>
            <td>Selenium</td>
            <td>1000</td>
         </tr>
         <tr>
            <td>Master In Java</td>
            <td>E</td>
            <td>JAVA</td>
            <td>2000</td>
         </tr>
         <tr>
            <td>Master In JS</td>
            <td>F</td>
            <td>Javascript</td>
            <td>1000</td>
         </tr>
      </table>
   </body>
</html>


Save the code as “table.html”, then you will get an HTML table like below.

 

To  print the total cost of all the books listed in the table:

  1. Open URL 
  2. Find the x-path
  3. compute the cost for each book,

Find the X-Path of the Table:

  • Go to the website
  • Right-click on the table and select inspect and copy the x-path.
  • If any doubts check this article 
  • For finding the price we have to copy the x-path for the 4th column ie. the price column

/html/body/table/tbody/tr/td[4]

Java




package GFG_Maven.GFG_MAven;
import java.util.List;
  
import org.openqa.selenium.By;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
  
public class Geeks {
    public static void main(String args[]) throws InterruptedException {
        
        System.setProperty("webdriver.chrome.driver","C:\\Users\\ADMIN\\Documents\\chromedriver.exe");
        ChromeDriver driver = new ChromeDriver();
            
        // Maximize the browser
        driver.manage().window().maximize();
    
        // Launch Website
        driver.get("file:///C:/Users/ADMIN/Desktop/table.html");
        List<WebElement> costColumns= driver.findElements(By.xpath("/html/body/table/tbody/tr/td[4]"));
          
          int sum_price=0;
          
          for(WebElement e : costColumns)
        {
           sum_price= sum_price+Integer.parseInt(e.getText());
        }
        
        System.out.println("total price: "+sum_price);
          
    }
  
}


Step by Step Code Explanation:

  1. In setup method do all the config stuff, like launch a browser, open URL.
  2. To get the URL you should define the address of the file which  saved as table.html in your system
  3. Creating a List of data type web elements and storing all the values in the 4th column in the table
  4.  Now looping through the List and convert the data into integer and sum the price.
  5. Print the total cost
  6. Then click on run java application in eclipse.
     

For setting the chrome driver and selenium webdriver refer to this article How to Open Chrome Browser Using Selenium in Java?

Output:



Similar Reads

Differences Between 'Selenium-server-standalone.jar' and 'Selenium Client and WebDriver'
In web application testing, Selenium has emerged as one of the most basic foundations, providing robust mechanisms for automating browsers. Among its suite, two elements stand out, which are: 'Selenium-server-standalone.jar' and 'Selenium Client and WebDriver'. Both 'Selenium-server-standalone.jar' and 'Selenium Client and WebDriver' are very impor
4 min read
Find Web Elements using Selenium WebDriver
We can identify web elements in the web page using the following two tools: Developer Tools FireBug and FirePath Developer Tools - Right-click on the web page. Navigate to inspect element to find the developer's tool. Note: There are some websites where the right-click is disabled. eg. IRCTC, banking websites, etc. In order to open the developer's
4 min read
How to Check the Accessibility of the Static and Non-Static Variables by a Static Method?
The static keyword is a non - access modifier in Java which can be used for variables, methods, and block of code. Static variables in Java belong to the class i.e it is initialized only once at the start of the execution. By using static variables a single copy is shared among all the instances of the class, and they can be accessed directly by cl
3 min read
How to Drag and Drop an Element using Selenium WebDriver in Java?
Selenium is an open-source web automation tool that supports many user actions to perform in the web browser. Automating a modern web page that has a drag and drop functionality and drag and drop is used to upload the files and so many user activities. so to perform the drag and drop actions the selenium provides a class called Actions. The Action
2 min read
How to Perform Drag and Drop Offset Method in Selenium WebDriver using Java?
Selenium is an open-source web automation tool that supports many user actions to perform in the web browser. Automating a modern web page that has a drag and drop functionality and drag and drop is used to upload the files and so many user activities. so to perform the drag and drop actions the selenium provides a class called Actions. The Action
2 min read
Understand Java Interface using Selenium WebDriver
Java Interface is a blueprint of the class that contains static constants and abstract methods. It cannot have a method body. The interface is a mechanism to achieve abstraction. This article focuses on discussing the Selenium WebDriver Interface. Example: public interface Animal { void printAnimalName(); void printAnimalColor(); void printAnimalWe
4 min read
How to Take a Screenshot in Selenium WebDriver Using Java?
Selenium WebDriver is a collection of open-source APIs that are used to automate the testing of a web application. To capture a screenshot in Selenium, one has to utilize the method Takes Screenshot. This notifies WebDriver that it should take a screenshot in Selenium and store it. Table of Content Why take screenshots in Selenium?How to take scree
3 min read
How to open a new tab using Selenium WebDriver in Java?
An object-oriented platform-independent language that is used for creating applications is known as Java. Sometimes the applications are so robust that we need some automation to test their application. This can be done using various automation tools such as Selenium. While doing automation, there also occurs the need to open a new tab using the Se
5 min read
How to Install Selenium WebDriver on Windows for Java?
Selenium is an open-source tool for automating testing of web applications which is free and it supports various programming languages such as C#, Java, Perl, PHP, Python, and Ruby. Selenium Webdriver is most well known or you can say famed with Java as of now. In this article, we will look into how to install selenium Webdriver on a Windows operat
3 min read
How to ask the Selenium-WebDriver to wait for few seconds in Java?
An open-source framework that is used for automating or testing web applications is known as Selenium. There are some circumstances when the particular component takes some time to load or we want a particular webpage to be opened for much more duration, in that case, we ask the Selenium web driver to wait for a few seconds. In this article, we wil
9 min read
Article Tags :
Practice Tags :