import org.testng.annotations.AfterTest;
import org.testng.annotations.BeforeTest;
public class testpins1 {
public static void main(String[] args) throws Exception {
testpins1 tp =new testpins1();
tp.setup();
tp.Handle_Dynamic_Webtable();
tp.tearDown();
}
WebDriver driver = new FirefoxDriver();
@BeforeTest
public void setup() throws Exception {
driver.manage().window().maximize();
driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);
driver.get("
http://www.moneycontrol.com/");
}
@AfterTest
public void tearDown() throws Exception {
driver.quit();
}
@Test
public void Handle_Dynamic_Webtable() throws FileNotFoundException {
FileOutputStream fos = new FileOutputStream("C://WebTableTOSpreedsheet.xls");
XSSFWorkbook wkb = new XSSFWorkbook();
XSSFSheet sheet1 = wkb.createSheet("DataStorage");
WebElement mytable = driver.findElement(By.xpath(".//*[@id='mc_mainWrapper']/section/div/div[2]/aside/div[3]/div[2]/div[1]/table"));
List<WebElement> rows_table = mytable.findElements(By.tagName("tr"));
int rows_count = rows_table.size();
System.out.println("Number of Rows " + rows_count);
for (int row = 0; row < rows_count; row++) {
XSSFRow excelRow = sheet1.createRow(row);
if(row==0){
List<WebElement> head_row = rows_table.get(row).findElements(By.tagName("th"));
int Head_count = head_row .size();
System.out.println("Number of Header cells In Row 0 are "+ Head_count);
for(int i=0;i<Head_count;i++) {
XSSFCell excelCell = excelRow.createCell(i);
excelCell.setCellType(XSSFCell.CELL_TYPE_STRING);
String celtext = head_row.get(i).getText();
excelCell.setCellValue(celtext);
System.out.println("Header in valuein column number " + i + " Is " + celtext);
}
}else{
List<WebElement> Columns_row = rows_table.get(row).findElements(By.tagName("td"));
int columns_count = Columns_row.size();
System.out.println("Number of cells In Row " + row + " are "+ columns_count);
for (int column = 0; column < columns_count; column++) {
XSSFCell excelCell = excelRow.createCell(column);
excelCell.setCellType(XSSFCell.CELL_TYPE_STRING);
String celtext = Columns_row.get(column).getText();
excelCell.setCellValue(celtext);
System.out.println("Cell Value Of row number " + row+ " and column number " + column + " Is " + celtext);
}
}
System.out.println("--------------------------------------------------");
}
try {
fos.flush();
wkb.write(fos);
fos.close();