Dynamic Web Table handle using selenium :
What Is Web Table ?
- Web Table is made of rows and columns with cell.
- When we create a table for a web page that is called as a web table.
- Web tables are basically a set of elements that are stored in logical row and column format.
- This is used to organize similar information on a web page.
- In HTML, table is created using <table> tag.
Web Table Consist :
- ’table’ tag defines HTML table.
- ’tbody’ tag defines a container for rows and columns.
- ’tr’ defines rows in an HTML table.
- ’td’/’th’ define the column of an HTML table.
Types Of Web Tables :
1. Static table:
- Data is static within cell i.e. Number of rows and columns are fixed.
- If there are only one row of the same rows in the web table and the same cell number, whenever you load the page every line.
2. Dynamic table:
- Data is dynamic within cell i.e. Number of rows and columns are NOT fixed.
- When you load the pages of the software web application, the rows and columns of the table are rising / decreasing or some lines have more cells and few lines contain fewer cells.
HTML Table Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 | <html> <head> <title>HTML Table</title> </head> <body> <table border = "1"> <tr> <td>Row 1, Column 1</td> <td>Row 1, Column 2</td> </tr> <tr> <td>Row 2, Column 1</td> <td>Row 2, Column 2</td> </tr> </table> </body> </html> |
Scenarios :
- Launch Browser.
- Open URL.
- Identify table using ID where we want to perform Operation’s.
- Read all tr (Table row) and td (Table Data).
- Read each and every cell record.
- Compare / Verify every Actual cell record with Expected cell.
- If found Expected cell within table then Print (“Record is Found…”)
Check Cell present or Not in Dynamic WebTable Handle using Selenium:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 | 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 Print_WebTable_Data { public static void main(String args[]) { System.setProperty("webdriver.chrome.driver", "Path to your chromedriver.exe"); WebDriver driver = new ChromeDriver(); driver.get("https://www.stqatools.com"); driver.manage().window().maximize(); // Identify the table <a href="https://stqatools.com/selenium-webelement-commands/">WebElement</a> table = driver.findElement(By.id("tableId")); // Get all rows (tr tags) List<WebElement> rows = table.findElements(By.tagName("tr")); String Expected = "CellName"; // Print data from each row (Data from each td tag) for (WebElement row : rows) { List<WebElement> cols = row.findElements(By.tagName("td")); for (WebElement col : cols) { System.out.print(col.getText() + "\t"); String Actual = col.getText(); // Check Expected Cell is present or not in WebTable if (Actual.equals(Expected)) { System.out.println("Cell Exist in WebTable..."); } } System.out.println(); } } } |
Dynamic Web Table handle using selenium and Dynamic Web Table handle using selenium and Dynamic Web Table handle using selenium