MainMenu

Home Core Java Overview Maven Tutorials

Saturday, 14 January 2017

Get Webpage table in xlsx file in selenium

import java.io.FileOutputStream;
import java.io.IOException;
import org.apache.poi.xssf.usermodel.XSSFCell;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
import java.util.List;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
public class CSC
{
WebDriver driver;
XSSFWorkbook wb1;
XSSFSheet st1;
FileInputStream fis1;
public static void main(String args[]) throws IOException, InterruptedException
{
CSC obj = new CSC();
System.out.println("table is printed");
}
public CSC() throws IOException, InterruptedException
{
driver = new FirefoxDriver();
driver.navigate().to("http://www.allcompanydata.com/in/search/?code=aaa&option=company&page=1");
Thread.sleep(2000);
WebElement data1 = driver.findElement(By.tagName("table"));
List tableRows = data1.findElements(By.tagName("tr"));
int rowSize=tableRows.size();
System.out.println(rowSize);
fis1=new FileInputStream("D:\\study material\\Selenium Final complete\\allcompanydata\\aaa.xlsx");
wb1 = new XSSFWorkbook(fis1);
st1 = wb1.createSheet("csc");
for (int i = 0; i {
WebElement webRow = tableRows.get(i);
List allcells = webRow.findElements(By.tagName("td"));
if(allcells.size()>1)
{
XSSFRow row = st1.createRow(i);
for(int j =0; j {
WebElement webCell = allcells.get(j);
String text = webCell.getText();
XSSFCell cell = row.createCell(j);
cell.setCellValue(webCell.getText());
FileOutputStream fos = new FileOutputStream("D:\\study material\\Selenium Final complete\\allcompanydata\\aaa.xlsx");
wb1.write(fos);
}
}
}
}
}

No comments:

Post a Comment