package com.automation.tests; import org.openqa.selenium.By; import org.openqa.selenium.WebDriver; import org.openqa.selenium.WebElement; import org.testng.annotations.AfterTest; import org.testng.annotations.BeforeTest; import org.testng.annotations.Test; import utility.Helper; public class TableDemo { WebDriver driver; @Test public void readingTableData(){ int rows = driver.findElements(By.xpath("//tbody[@id='table1']/tr")).size(); int columns = driver.findElements(By.xpath("//tbody[@id='table1']/tr[1]/th")).size(); System.out.println("Number of Rows are: " + rows); System.out.println("Number of Columns are: " + columns); //Printing the Table Heading for (int i = 1; i <= columns; i++) { WebElement data = driver.findElement(By.xpath( "//tbody/tr[1]" + "/th[" + i + "]")); System.out.print(data.getText() + "\t"); } System.out.println(); //Printing the Table Data for (int i = 2; i <= rows; i++) { for (int j = 1; j <= columns; j++) { WebElement data = driver.findElement(By.xpath( "//tbody/tr[" + i + "]" + "/td[" + j + "]")); System.out.print(data.getText() + "\t"); } System.out.println(); } } @AfterTest public void afterReadingTableData() { driver.close(); } @BeforeTest public void beforeReadingTableData(){ driver = Helper.launchBrowser("chrome"); driver.get("http://anish-selenium.blogspot.in/"); } }
Reading HTML table data
Subscribe to:
Posts (Atom)
No comments:
Post a Comment