Handle Dynamic Table in WebDriver

0


The table is used to contain the records based on rows and columns, rows will be presented by <tr> and columns will be presented by <td>.
Structure of Table:-
<table>
<thead>
<tr><th>SNO</th><th>Name</th></tr>
</thead>
<tbody>
<tr><td>1001<td><td>XYZ</td></tr>
<tr><td>1002<td><td>ABC</td></tr>
<tr><td>1003<td><td>MNO</td></tr>
<tr><td>1004<td><td>KLMN</td></tr>
<tr><td>1005<td><td>TUV</td></tr>
</tbody>
</table>
What is Dynamic Table:-
If a number of rows and columns is not fixed then it is called a dynamic table, we can also retrieve a record from Web Driver Code to dynamic Table.
Dynamic table data will be filled by the database means its rows can be changed dynamically.
Q) CREATE AUTOMATION SCRIPT to COUNT NUMBER OF ROWS and COLUMN using selenium Web Driver?
import java.util.List;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
public class DynamicTableExample {
public static void main(String[] args) {
    System.setProperty("webdriver.chrome.driver","c:\\chromedriver.exe");
    WebDriver driver = new ChromeDriver();
    driver.get("http://demo.guru99.com/test/web-table-element.php");
    List col = driver.findElements(By.xpath("//*[@id=\"leftcontainer\"]/table/thead/tr/th"));
List row = driver.findElements(By.xpath("//*[@id=\"leftcontainer\"]/table/tbody/tr/td[1]"));
System.out.print("number of column is "+col.size());
System.out.print("number of rows is "+row.size());
}
}
Create Automation Script to find max price in Dynamic Table?
Answer:-
package scs;
import java.text.ParseException;
import java.util.List;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
public class DynamicTableExampleNew {
public static void main(String[] args) {
System.setProperty("webdriver.chrome.driver","c://chromedriver.exe");
WebDriver wd= new ChromeDriver();
wd.get("http://demo.guru99.com/test/web-table-element.php");
// String max;
double m=0,r=0;
      //No. of Columns
List  col = wd.findElements(By.xpath(".//*[@id='leftcontainer']/table/thead/tr/th"));
System.out.println("Total No of columns are : " +col.size());
       //No.of rows
List  rows = wd.findElements(By.xpath (".//*[@id='leftcontainer']/table/tbody/tr/td[1]"));
System.out.println("Total No of rows are : " + rows.size());
for (int i =1;i<rows.size();i++)
       {    
          String max= wd.findElement(By.xpath(".//*[@id='leftcontainer']/table/tbody/tr["+i+"]/td[4]")).getText();
          // System.out.println(max);
           m = Double.parseDouble(max);
          if(m>r)
            {    
               r=m;
           }
       }
       System.out.println("Maximum current price is : "+ r);

}
}
Example of sum of columns in Dynamic Table in Webdriver script?
package scs;
import java.util.List;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
public class TableExample {
public static void main(String[] args) {
System.setProperty("webdriver.chrome.driver","c:\\chromedriver.exe");
    WebDriver driver = new ChromeDriver();
    driver.get("http://demo.guru99.com/test/web-table-element.php");
            List col=driver.findElements(By.xpath("//*[@id='leftcontainer']/table/thead/tr/th"));
    System.out.println(col.size());
    List<WebElement> row=driver.findElements(By.xpath("//*[@id='leftcontainer']/table/tbody/tr/td[4]"));
    System.out.println(row.size());
   float sum=0;
    for(WebElement we:row)
    {
    sum = sum + Float.parseFloat(we.getText());
    System.out.print(we.getText());
    }
    System.out.println("Sum is "+sum);
            driver.close();
}
}



Post a Comment

0Comments

POST Answer of Questions and ASK to Doubt

Post a Comment (0)