Friday, April 27, 2018

How to handle a Web Table with Selenium Web Driver?

How to handle a Web Table with Selenium Web Driver?


Solution:

Web Table : A table is collection of rows and columns, table created on web is known as Web Table.
The <table> tag defines the HTML table where we use the <tr> tag for rows , the <td> tag for columns and the element defines a table header.

There are two types of Web Table :

a). Static Table :  A table where number of rows and number of columns are fixed. E.g :- Days of week table.

b). Dynamic Table : A table where number of rows and number of columns are dynamic means it may vary as per data. E.g:- Match Score Card and Organisational Sales Chart.

We will learn the following things now:

1). How to print all the data from table?
2). How to print data of specific column ?
3). How to print data of specific row?
4). How to print data of specific cell based data of another cell?
5.) How to find & print sum of all of values of column?.
6.) How to find the shares whose Current Price is less than Rs. 800?

Let Say we have a following table.
CompanyGroupPrev Close (Rs)Current Price (Rs)% Change
L&T Infotech A 1,539.15 1,716.85 + 11.55
Indiabulls Ventures A 428.70 470.40 + 9.73
NIIT Technologies A 998.55 1,080.90 + 8.25
Raymond Ltd A 1,065.50 1,132.35 + 6.27
Persistent Systems A 725.85 770.00 + 6.08
L&T Technology Serv A 1,221.35 1,289.80 + 5.60
Religare Enterprises A 56.15 58.95 + 4.99
Indiabulls Real Estate A 196.80 204.90 + 4.12
Dewan Housing A 585.10 608.55 + 4.01

First, How to print all the data from table in console ? 
Solution:-
WebDriver driver;
System.setProperty("webdriver.chrome.driver", "C:/Users/User/Downloads/chromedriver/chromedriver.exe");
driver = new ChromeDriver();

//Open URL
driver.get("any**webtable**url");

// Now locate the tables rows and create List of webelements
List tableRows=driver.findElements(By.xpath("//*[@id='leftcontainer']/table/tbody/tr"));
  int rowSize = rows.size();
  
  for(int i=0;i<rowSize;i++){
   
   List<WebElement> cells = driver.findElements(By.xpath("//table[@class='dataTable']/tbody/tr["+i+"]/td"));
   int cellSize = cells.size();
   for(int j=0;j<cellSize;j++){
    System.out.println(cells.get(j).getText());
   }
  }
Second, How to print data of specific column ?
 List<WebElement> cell= driver.findElements(By.xpath("//table[@class='dataTable']/tbody/tr/td[3]"));
         int cellSize = cell.size();
          for(int i=0;i < cellsize; i++){
            System.out.println(cell.getText());
               }
Third, How to print data of specific row? ?
 List<WebElement> row= driver.findElements(By.xpath("//table[@class='dataTable']/tbody/tr[3]"));
         int rowSize = row.size();
          for(int i=0;i < rowSize; i++){
            System.out.println(row.getText());
               }
Fourth, How to print data of specific cell based data of another cell?
List<WebElement> rowList = driver.findElements(By.xpath("//*[@id='leftcontainer']/table/tbody/tr"));
   for(int i = 0;i < rowList.size();i++){

            String rowText = rowList.get(i).getText();

//Now, we will check if String contains value "NIIT", then we will find its Current Price  
     if(rowText.contains("NIIT")){

      List<WebElement> list = driver.findElements(By.xpath("//*[@id='leftcontainer']/table/tbody/tr/td[3]"));

      String cellText=list.get(i).getText();

      System.out.println(cellText);

   }
}
Fifth, How to find & print sum of all of values of column?.
                
List<WebElement> currentPrice = driver.findElements(By.xpath("//table[@class='dataTable']/tbody/tr/td[3]"));

  double sum = 0;

  for(int i=0;i < currentPrice.size();i++){

   sum = sum + Double.parseDouble(currentPrice.get(i).getText().replace(",", ""));

  }
  System.out.println("Total Value of all shares current price is : " + sum);

Sixth, How to find the shares price whose Current Price is less than Rs. 800?
                
List<WebElement> currentPrice = driver.findElements(By.xpath("//table[@class='dataTable']/tbody/tr/td[3]"));


  for(int i=0;i < currentPrice.size();i++){

  String refineValue = currentPrice.get(i).getText().replace(",", "");

   if(Double.parseDouble(refineValue) < = 800.00){
    System.out.println("Share Price is : " + refineValue);
   }

  }

No comments:

Post a Comment

Questions Asked in Interview for Manual Testing Engineer?

1. What is Boundary Value Analysis and Equivalence Partitioning ? 2. What is Responsive web design ? 3. What is Globalization Te...