How to Handle Web Table In Selenium? – Devstringx

Back to Blog

How to Handle Web Table In Selenium? – Devstringx

Website tables are an important way to provide information to users.  Automation of the table extraction process has applications in web mining, information retrieval, knowledge acquisition, and summarization. The article discusses information extraction from HTML tables embedded in web pages.

Nowadays, All enterprise applications like Customer Relationship Management (CRM) Software, Project Management Tools, Marketing Automation, Enterprise Resource Planning (ERP) Software, and Treasury Management systems (TMS).

The Data are displayed in the form of rows and columns. The data displayed can either be static or dynamic in nature. So, this arrangement of data we called Web Table. Web tables are mainly used in scenarios where you need to display the data or information in tabular format.

Web Table As Given Below As:

This table shows the information on the different monuments of different countries and where these are located and their rank, size, height, and built.

Web Table

Appropriate Use of Web Tables

Tables can be useful to present tabular data in an organized way when dealing with the following types of data:

  1. Quantitative Information – Tables show a certain quantity, range, or amount of information retrieval, knowledge acquisition, and summarization.
  2. Data Comparison – Tables show different amounts of data. It shows a comparison of different data like in the above table shows different monument information.
  3. If you have more than one set of values that are directly related.

HTML Tables

In HTML the table contents confine to the body of text within <table> and </table> Html tags. A web Table is a web element like a textbox, checkbox, or radio button. <table>is the HTML tag that represents the table. While <th> represents the heading of a table, <tr> represents the row of the table and <td> represents the column of the table.

Example of Writing a Web Table Using HTML:

<table><tr>

<th>Country</th>

</tr>

<tr>

<td>UAE</td>

<td>Saudi Arabia</td>

<td>Taiwan</td>

<td>china</td>

</tr>

</table>

Selenium is a widely used automation suite. But there is no method to handle the web table. If you want to get the values of the table. Then you have to use XPath.

There are Two Types of Web Tables
  1. Static Web Tables= the data remain fixed not changed. Its row and column are not updated
  2. Dynamic Web Tables= the data changes according to the requirement. Its row and column change depending on the data shifts

Read Also – Get The Execution Time of Multiple Methods In Selenium Java

How to handle webtable in selenium and get different values in the table?
  1. Launch new Browser
  2. Open URL “https://www.techlistic.com/p/demo-selenium-practice.html
  3. Suppose you want to get the values of the ‘country’ column

//table[@summary=’Sample Table’]//tbody//tr[‘row_number’]//td[(text())][‘column_number’]

For example, the country column number is 1

//table [@summary=’SampleTable’]//tbody//tr[‘row_number’]//td[(text())][1]

  1. First, you get the values of all rows of web element
WebDriverManager.chromedriver().setup();WebDriverdriver=newChromeDriver();

driver.get(“https://www.techlistic.com/p/demo-selenium-practice.html”);

driver.manage().window().maximize();

List<Web Element>country_list = driver.findElements(By.xpath(“//table[@summary=’Sample Table’]//tbody//tr//td[(text())][1]”));

2. Using for loop to read all the values of the table

for(int i=1;i<=country_list.size();i++) {String country_text=driver.findElement(By.xpath(“//table[@summary=’Sample Table’]//tbody//tr[“+i+”]//td[(text())][1]”));

}

3. Using the get text method to get the values

String Country_Name=country_text.getText();System.out.println(“Country Name =”+country_text);

Selenium Related Articles

 

Share this post

Back to Blog