Working with WebTables in Selenium

In the previous post, we have seen how to work with the different kind of Pop-ups in Selenium.In this post,we will see the working with WebTables in Selenium

WebTable is a very common feature you will find almost in every application. It contains a combination of rows and columns. Sometimes, there are requirements like you need to read the data from the webtables and use it as an output for some other functions , verify that data is correctly populated or not in the webtables etc.

We will take the example of a Webtable present in the the ‘www.testingpool.com‘. You can access this webtable at https://testingpool.com/data-types-in-java/“.

This webtable displays the information about different data types in java.

webtable handling in selenium

WebTable

In WebTable , we can perform operations like counting total no. of rows, counting total no. of columns, extracting data etc. Let’s understand this with examples.

In HTML , Table is created by using the below commonly used tags (Explained only those tags used in our demo table.)

  • table: represent table
  • tbody: represent table body
  • tr: table row
  • td: table column

Don't Forget Note Means Important Remember Forgetting

1 tr tag(rows) can have multiple td tags(columns).

Webtable example in selenium

 


 

How to count no. of rows in a WebTable?

We will create a Xpath to count the no. of tr tags in a table.

XPATH to count no. of rows:

//div[@class=’su-table’]/table/tbody/tr

As you can see in the below figure, this xpath returns total 9 rows.

Count no. of rows in webtable selenium


 

How to count no. of columns in a WebTable?

We will create a Xpath to count the no. of td tags in a tr tag.

XPATH to count no. of columns:

//div[@class=’su-table’]/table/tbody/tr[1]/td

As you can see in the below figure, this xpath returns total 4 colums.

Coulmn count i a webtable selenium


 

Extract the data from the WebTable:

For extracting the data from WebTable, we will count no. of rows and columns. Then, we create nested for loop by using no. of rows and columns and use the method ‘getText()’ to extract the data.

public class WebTableExample {
		private static WebDriver driver;
	public static void main(String[] args) throws InterruptedException {
			
			driver = new FirefoxDriver();
			
			driver.manage().window().maximize();
			driver.get("https://testingpool.com/data-types-in-java/");
			//count rows
			List<WebElement> Rows = driver.findElements(By.xpath("//div[@class='su-table']/table/tbody/tr"));
			int totalRows = Rows.size();
			System.out.println(" Total rows : "+totalRows);
			
			//count columns
			List<WebElement> Columns = driver.findElements(By.xpath("//div[@class='su-table']/table/tbody/tr[1]/td"));
			int totalColumns = Columns.size();
			System.out.println(" Total Columns : "+totalColumns);
			
			//Extract data
			for(int i=1;i<=totalRows;i++){
				for(int j=1;j<=totalColumns;j++){
					WebElement dataCell = driver.findElement(By.xpath("//div[@class='su-table']/table/tbody/tr["+i+"]/td["+j+"]"));
					System.out.println(dataCell.getText());
				}
			}
	 }
}

Output: 

Total rows : 9
Total Columns : 4
Datatype Description Example Default value
byte Can be used for saving memory in large arrays.It is four times smaller than an int. byte a =100 0 short Can be used for saving memory in large arrays.It is two times smaller than an int. short x=1000 0 int int is generally use as default data type,unless there is memory concern int x=1000 0 long It is used when larger value than int is needed. int x=100000L 0L float use it if needs to save memory in large arrays of floating point numbers. int x=176.8f 0.0f double It is generally used as default value for decimal values. int d=176.8 0.0d boolean Its 2 possible value are – true and false. boolean name=true false char Char data type is used to store any character. char c =”A” ‘\u0000?
Avatar photo

Shekhar Sharma

Shekhar Sharma is founder of testingpool.com. This website is his window to the world. He believes that ,"Knowledge increases by sharing but not by saving".

You may also like...

3 Responses

  1. raghu says:

    Hi
    I have a question. How to store same table in HashMap?
    Can you please explain?
    Eg: Key as “Datatype”
    Value as “Description,Example,DefaultValue”

  2. Raghu, Please find the code below where you can store first column as key and remaining columns as values in Hashmap. Hope, it helps.

    public class HashmapExample {
    private static WebDriver driver;

    public static void main(String[] args) {

    System.setProperty("webdriver.chrome.driver", System.getProperty("user.dir")+"/chromedriver");
    driver = new ChromeDriver();

    driver.manage().window().maximize();
    driver.get("https://testingpool.com/data-types-in-java/");
    //count rows
    List Rows = driver.findElements(By.xpath("//div[@class='su-table']/table/tbody/tr"));
    int totalRows = Rows.size();
    System.out.println(" Total rows : "+totalRows);

    //count columns
    List Columns = driver.findElements(By.xpath("//div[@class='su-table']/table/tbody/tr[1]/td"));
    int totalColumns = Columns.size();
    System.out.println(" Total Columns : "+totalColumns);

    HashMap map1 = new HashMap();

    //Extract data
    for(int i=1;i<=totalRows;i++){ //Fetch all column's values to different variables and perform desired operations WebElement strdatatype = driver.findElement(By.xpath("//div[@class='su-table']/table/tbody/tr["+i+"]/td[1]")); WebElement strDesc = driver.findElement(By.xpath("//div[@class='su-table']/table/tbody/tr["+i+"]/td[2]")); WebElement strExample = driver.findElement(By.xpath("//div[@class='su-table']/table/tbody/tr["+i+"]/td[3]")); WebElement strDefaultVal = driver.findElement(By.xpath("//div[@class='su-table']/table/tbody/tr["+i+"]/td[4]")); //First Column - Key String myKey = strdatatype.getText(); // Value - Combination of remaining 3 columns String itsValue = strDesc.getText() +"," + strExample.getText() + "," + strDefaultVal.getText(); map1.put(myKey, itsValue); } for (Map.Entry entry : map1.entrySet()) {
    System.out.println("***The Key is - "+ entry.getKey()+"***");
    System.out.println("The value is - " + entry.getValue());
    }
    driver.quit();
    }

    }

  1. August 18, 2015

    […] the previous post, we have seen how to work with WebTables in selenium. In this post, we will learn how to utilize the property files to create an object […]